News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Convert / Get Numeric Date from Date

Started by kevin, May 28, 2008, 09:19:08 PM

Previous topic - Next topic

kevin

 This function converts a DATE string from the default format of Day Month Year (ie 10 May 2005) to  10,5,2005



PlayBASIC Code: [Select]
   Print GetNumericDate(currentdate$())
Print GetNumericDate("10/jan/2007")
Print GetNumericDate("12/feb/2008")
Print GetNumericDate("13/mar/2009")
Print GetNumericDate("14/apr/2010")
Print GetNumericDate("15/jun/2011")
Print GetNumericDate("16/jul/2012")
Print GetNumericDate("17/aug/2012")
Print GetNumericDate("19/sep/2013")
Print GetNumericDate("20/oct/2014")
Print GetNumericDate("21/nov/2015")
Print GetNumericDate("21/dec/2016")

sync
waitkey



// This function converts a dates format from eg 1 May 2000 to 1,5,2000

Function GetNumericDate(THisdate$)

ListOFmonths$="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"

DIm _GetNumericDate$(3)

Thisdate$=trim$(thisdate$," "+chr$(9))
// replace delimiters chr's
Thisdate$=replace$(thisdate$," ","/",1,false,false)
Thisdate$=replace$(thisdate$,"/",",",1,false,false)

// Split the feilds to an array
if splittoarray(Thisdate$,",",_GetNumericDate$(),1)=3
pos=INstring(lower$(listofmonths$),lower$(_GetNumericDate$(2)),1,false)
Result$=_GetNumericDate$(1)+","+str$(((pos-1)/4)+1)+","+_GetNumericDate$(3)
endif
unDIm _GetNumericDate$()

EndFunction Result$



kevin

#1
  This version converts a string containing a date, formatted as YYYY-MM-DD, to DD,MMM,YYYY



PlayBASIC Code: [Select]
   print MonthFormatDate("2000-1-30")
print MonthFormatDate("2000-10-1")

Sync
waitkey

// This function converts a numeric dates formated as yyyy,mm,dd to dd,mmm,yyyy
// the month will be in 3 letter tag

Function MonthFormatDate(THisdate$)

Dim _MonthNames$(12)
ListOFmonths$="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
count=SplitToArray(ListOfMonths$,",",_MonthNames$(),1)

DIm _GetNumericDate$(3)

// strip any pre/post spaces around the date
Thisdate$=trim$(thisdate$," "+chr$(9))
// replace delimiters chr's
Thisdate$=replace$(thisdate$," ",",",1,false,false)
Thisdate$=replace$(thisdate$,"/",",",1,false,false)
Thisdate$=replace$(thisdate$,"-",",",1,false,false)

// Split the feilds to an array
if splittoarray(Thisdate$,",",_GetNumericDate$(),1)=3
m=val(_GetNumericDate$(2))
Result$=_GetNumericDate$(3)+","+_MonthNames$(m)
Result$=Result$+","+_GetNumericDate$(1)
endif
unDIm _GetNumericDate$()

EndFunction Result$




kevin

#2
 This example further expands upon the previous examples and add a function to convert a date formated DD,MMM,YEAR  (ie. 10,May,2003) to an approximate  time in history.  This is can useful for sorting information based upon date..


PlayBASIC Code: [Select]
   // Type to hold our dates
Type tDates
ThisDate$
DayInHistory
endType

// Array to house our dates
Dim Dates(0) as tDates


// Add a bunch of the dates to the array so we've something to test
AddDate(currentdate$())
AddDate("10/jan/2007")
AddDate("12/feb/2008")
AddDate("13/mar/2009")
AddDate("14/apr/2010")
AddDate("15/jun/2011")
AddDate("16/jul/2012")
AddDate("17/aug/2012")
AddDate("19/sep/2013")
AddDate("17/aug/1912")
AddDate("20/oct/2014")
AddDate("21/nov/2015")
AddDate("21/dec/2016")

// Display the dates
print " Unsorted DATES"
For lp=1 to GetArrayElements(Dates(),1)
Print Dates(lp).ThisDate$ +" "+Str$(Dates(lp).DayInHistory)
Next


// Display in order of OLDest to current
Display_Dates_OldToNew()

// Display them in backwards order
Display_Dates_NewToOld()


Sync
waitkey



Function Display_Dates_OldToNew()
print " Sorted Dates (Forwards)"
max= GetArrayElements(Dates(),1)
Dim SortedMask(Max)
For lp=1 to Max
// Find Smallest
DayInHistory=9999999
DayInHistoryIndex=-1
for Smallest=1 to Max
If SortedMask(Smallest)=false
if Dates(Smallest).DayInHistory<=DayInHistory
DayInHistoryIndex=Smallest
DayInHistory=Dates(Smallest).DayInHistory
endif
endif
next
SortedMask(DayInHistoryIndex)=True
Print Dates(DayInHistoryIndex).ThisDate$ +" "+Str$(Dates(DayInHistoryIndex).DayInHistory)
Next
EndFunction


Function Display_Dates_NewToOld()
print " Sorted Dates (Backwards)"
max= GetArrayElements(Dates(),1)
Dim SortedMask(Max)
For lp=1 to Max
// Find Smallest
DayInHistory =-99999
DayInHistoryIndex =-1
for Largest=1 to Max
If SortedMask(Largest)=false
if Dates(Largest).DayInHistory>=DayInHistory
DayInHistoryIndex=Largest
DayInHistory=Dates(Largest).DayInHistory
endif
endif
next
SortedMask(DayInHistoryIndex)=True
Print Dates(DayInHistoryIndex).ThisDate$ +" "+Str$(Dates(DayInHistoryIndex).DayInHistory)
Next
EndFunction




Function AddDate(ThisDate$)
index=GetFreeCell(dates())
Dates(index).ThisDate$ =GetNumericDate(ThisDate$)
Dates(index).DayInHistory =ConvertDateToDayInHistory(THisdate$)

EndFunction

// This function converts a dates format from eg 1 May 2000 to 1,5,2000

Function GetNumericDate(THisdate$)

ListOFmonths$="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"

Dim _GetNumericDate$(3)

Thisdate$=trim$(thisdate$," "+chr$(9))

// replace delimiters chr's
Thisdate$=replace$(thisdate$," ","/",1,false,false)
Thisdate$=replace$(thisdate$,"/",",",1,false,false)

// Split the feilds to an array
if splittoarray(Thisdate$,",",_GetNumericDate$(),1)=3
pos=INstring(lower$(listofmonths$),lower$(_GetNumericDate$(2)),1,false)
Month$=str$(((pos-1)/4)+1)
Result$=_GetNumericDate$(1)+","+Month$+","+_GetNumericDate$(3)
endif
unDIm _GetNumericDate$()

EndFunction Result$, DayInHistory


// This function converts a date format from eg 1 May 2000 to an appoximate day in history. Useful for sorting

Function ConvertDateToDayInHistory(THisdate$)

ListOFmonths$="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"

Dim _GetNumericDate$(3)

Thisdate$=trim$(thisdate$," "+chr$(9))

// replace delimiters chr's
Thisdate$=replace$(thisdate$," ","/",1,false,false)
Thisdate$=replace$(thisdate$,"/",",",1,false,false)

// Split the feilds to an array
if splittoarray(Thisdate$,",",_GetNumericDate$(),1)=3
pos=INstring(lower$(listofmonths$),lower$(_GetNumericDate$(2)),1,false)
Day$=_GetNumericDate$(1)
Month$=str$(((pos-1)/4)+1)
Year$=_GetNumericDate$(3)

// To avoid havuing to worry about leap years and days per month, here we just assume
// that all months conatin 32 days.
DaysPerMonth =32
DaysPerYear = 12*DaysPerMonth

Login required to view complete source code
 

 

kevin

#3
 This is a slight variation of the function above, accept this one converts an input of format "DD MMM YYYY" (eg 20 May 2020) and converts it to YMD,   so 20 May 2020 would become 2020,05,20

I tend to use dates in this format for naming files, so the files system will sort those names in order

PlayBASIC Code: [Select]
   print CurrentDate$()
print GetNumericDateYMA(CurrentDate$())
Sync
waitkey


Function GetNumericDateYMA(THisdate$)

ListOFmonths$="Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"

Dim _GetNumericDate$(3)

Thisdate$=trim$(thisdate$," "+chr$(9))

// replace delimiters chr's
Thisdate$=replace$(thisdate$," ","/",1,false,false)
Thisdate$=replace$(thisdate$,"/",",",1,false,false)

// Split the feilds to an array
if splittoarray(Thisdate$,",",_GetNumericDate$(),1)=3
pos=INstring(lower$(listofmonths$),lower$(_GetNumericDate$(2)),1,false)
Month$=digits$(((pos-1)/4)+1,2)
Result$=_GetNumericDate$(3)+","+Month$+","+_GetNumericDate$(1)
endif
unDIm _GetNumericDate$()

EndFunction Result$