Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

FormatDate() Function


  • Please log in to reply
7 replies to this topic
beardboy
  • Members
  • 443 posts
  • Last active: May 27 2017 08:41 AM
  • Joined: 02 Mar 2004
With multiple scripts I was having to convert date formats so I created a Function that I would just #Include.

; Program: Format Date Function
; Author: beardboy
; Version: 00
; Last Modified: 2005.05.18
; Last Changes: First Version

/*
types
1 = 20050415
2 = 04/15/05 or 04/15/2005
3 = Apr-15-05 or Apr-15-2005
4 = 15-04-05 or 15-04-2005

example
date := FormatDate("01/05/1999",2,"dddd MMMM dd yyyy")
MsgBox, %date%
ExitApp
*/

FormatDate(date,intype,outtype)
{
  if intype = 1 ; 20050415
  {
    if date <>
      FormatTime, date, %date%, %outtype%
  }
  else if intype = 2 ; 04/15/05 or 04/15/2005
  {
    StringLeft, month, date, 2
    StringMid, day, date, 4, 2
    StringMid, year, date, 7, 4
    year := FormatYear(year)
    FormatTime, date, %year%%month%%day%, %outtype%
  }
  else if intype = 3 ; Apr-15-05 or Apr-15-2005
  {
    months = Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
    StringLeft, month, date, 3
    StringMid, day, date, 5, 2
    StringMid, year, date, 8, 4
    Loop, Parse, months, `,
    {
      if a_loopfield = %month%
      {
        month = %a_index%
        break
      }
    }
    if month < 10
      month = 0%month%
    year := FormatYear(year)
    FormatTime, date, %year%%month%%day%, %outtype%
  }
  else if intype = 4 ; 15-04-05 or 15-04-2005
  {
    StringLeft, day, date, 2
    StringMid, month, date, 4, 2
    StringMid, year, date, 7, 4
    year := FormatYear(year)
    FormatTime, date, %year%%month%%day%, %outtype%
  }
  return date
}

FormatYear(year)
{
  StringLen, length, year
  if length = 2
  {
    if year > 30
      year = 19%year%
    else
      year = 20%year%
  }
  return year
}
Edit: Added type 4.

thanks,
beardboy

neyon
  • Members
  • 34 posts
  • Last active: Nov 25 2009 06:15 PM
  • Joined: 29 May 2005
Hey Beardboy,

This function is very useful!

Only one question: here in Holland we use the dateformat:
dd-mm-yyyy so sept,25 2005 will be written as
25-09-2005.

Can you append this into your function?

Thanks,
Neyon

beardboy
  • Members
  • 443 posts
  • Last active: May 27 2017 08:41 AM
  • Joined: 02 Mar 2004
neyon, updated first post with Type 4.

thanks,
beardboy

Robert Carnegie
  • Members
  • 54 posts
  • Last active: Mar 10 2009 05:34 PM
  • Joined: 01 Jun 2005
I wonder if there is a general standard for a wider range of date and time formats, that you could usefully follow? I work with Microsoft SQL Server and it has over a dozen, described here:

http://msdn.microsof... ... o_2f3o.asp

I think some of these also differ on placement of the year 1, i.e. not based on Christian dates. Of course, many people worldwide aren't Christians. For that matter, I think that historical dates about five hundred years ago or more have the year starting in March in contemporary writing, so that after December 1400 came January 1400, February 1400, and March 1401 - but I may be dreadfully confused.

I also doubt that this necessarily describes a standard. For instance, Java, which I don't actually know, probably has a similar system, and maybe a more universal one. However, I did get as far as looking at Java's date-time variable, and I don't think it goes back as far even as one billion years, whereas the age of the universe is known to be just under 14 billion. A very poor effort! And as for the future - ! Well, perhaps we actually won't need too much of that... ;-)

neyon
  • Members
  • 34 posts
  • Last active: Nov 25 2009 06:15 PM
  • Joined: 29 May 2005
Thank you very much beardboy.
Ik will use your function in my scripts.

:)

Neyon

BETLOG
  • Members
  • 222 posts
  • Last active: Apr 07 2010 01:58 PM
  • Joined: 27 Nov 2006
I was using this today with various date formats, and discovered it's limitations fairly quickly: any deviation from the expected input (which may include the lack of leading zeros, or a 'different' delimiter) causes the script to output the CURRENT date...and if you aren't careful you can end up with a date that is not correct...but because a date is *present* it may go unnoticed.

I made some quick changes to the way it functions... but have not had tme/reason to implement it further...maybe a real coder can do this if it looks useful. (I just hack stuff up for my own use, but it's never pretty/elegant)
The first three "IfInString" lines should probably be split out into their own sub-function, or put in the start of FormatDate()
to avoid repetition....
...and FormatDay() and FormatMonth() could probably be combined into a single finction, as all they do is ensure the leading 0 is on the day/month.


;---betlog		input: YYYY.mm.dd  or YY.mm.dd  delimeter: can be . or - or /
  else if intype = 5 ;--- 2005.04.15 or 05.04.15
  {
	IfInString, date,.
		StringSplit, TheDate, date,.
	IfInString, date,-
		StringSplit, TheDate, date,-
	IfInString, date,/
		StringSplit, TheDate, date,/
		
	year = %TheDate1%
	year := FormatYear(year)
	month =  %TheDate2%
	month := FormatMonth(month)	
	day = %TheDate3%
	day := FormatDay(day)	
    FormatTime, date, %year%%month%%day%, %outtype%
  }

  return date
}

/*
 CHOP
*/

FormatMonth(month)
{
  StringLen, lengthM, month
  if lengthM = 1
      month = 0%month%
  return month
} 

FormatDay(day)
{
  StringLen, lengthD, day
  if lengthD = 1
      day = 0%day%
  return day
}

Nice work beardboy, this one is now part of my 'most useful' selection of scripts.

philz
  • Members
  • 89 posts
  • Last active: Mar 02 2009 06:23 AM
  • Joined: 05 Jun 2007
this is a great tool, the only thing i could ask for is for it to include time as well

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
If you haven't already, have a look at FormatTime.