Code archives/Algorithms/DateInt()
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This Function converts the time from "09 Jun 2008" format to the "20080609" format, as an int. This will allow you to see whether one date is after another. This can be used to stop a program from running before or after a certain date. | |||||
;Stops program running after a certain date If DateInt(CurrentDate()) >= DateInt("09 Jun 2008") End ;Stops Program running before certain date ;If DateInt(CurrentDate()) < DateInt("09 Jun 2008") End ;Program can only run on certain date ;If DateInt(CurrentDate()) <> DateInt("09 Jun 2008") End Function DateInt%(Date$) ;Local Variables ----- Local D$ Local M$ Local Y$ Local DateResult% ;===================== ;Get Day D = Left(Date,2) ;Get Month Select Mid$(Date,4,3) Case "Jan" M = "01" Case "Feb" M = "02" Case "Mar" M = "03" Case "Apr" M = "04" Case "May" M = "05" Case "Jun" M = "06" Case "Jul" M = "07" Case "Aug" M = "08" Case "Sep" M = "09" Case "Oct" M = "10" Case "Nov" M = "11" Case "Dec" M = "12" End Select ;Get Year Y = Right(Date,4) ;Result Date DateResult = Int (Y + M + D) ;Return Result Date Return DateResult End Function |
Comments
| ||
Y=2008 M=06 D=09 Y+M+D = 2023. ??? Or, it must be adding them as a string before before being converted to Int? |
| ||
its the Int(Y + M + D) your converting to ints so TaskMaster is correct it just returns the added up total instead of the string :) |
| ||
It doesn't on any system I have tried it on, Works fine. Do a debuglog or print on the returned value to see, it will return 20080609 |
| ||
Boomboom is right. Works here. |
| ||
Local D$ Local M$ Local Y$ Adding strings = concatenation. |
| ||
Is there going to be a problem on non-English systems, or do the month abbreviations stay the same? |
| ||
I would assume they stay the same as they would be built into blitz. But I am not too sure on that tbh, This function was for a very small program that I know was only going out to english systems. Be nice if someone could test it :) |
| ||
Function DateInt%(Date$) Select Mid(Date,4,3) Case "Jan" Return(Left(Date,2)+"01"+Right(Date,4)) Case "Feb" Return(Left(Date,2)+"02"+Right(Date,4)) Case "Mar" Return(Left(Date,2)+"03"+Right(Date,4)) Case "Apr" Return(Left(Date,2)+"04"+Right(Date,4)) Case "May" Return(Left(Date,2)+"05"+Right(Date,4)) Case "Jun" Return(Left(Date,2)+"06"+Right(Date,4)) Case "Jul" Return(Left(Date,2)+"07"+Right(Date,4)) Case "Aug" Return(Left(Date,2)+"08"+Right(Date,4)) Case "Sep" Return(Left(Date,2)+"09"+Right(Date,4)) Case "Oct" Return(Left(Date,2)+"10"+Right(Date,4)) Case "Nov" Return(Left(Date,2)+"11"+Right(Date,4)) Case "Dec" Return(Left(Date,2)+"12"+Right(Date,4)) End Select End Function increase from 400 ms to 300 ms for 100000 loop tests |
| ||
Cool, if you really need the speed (not sure why) But you didn't flip around the year and day, in order to it to be used for its original purpose (determining if a date is before or after another one). Function DateInt%(Date$) Select Mid(Date,4,3) Case "Jan" Return(Right(Date,4)+"01"+Left(Date,2)) Case "Feb" Return(Right(Date,4)+"02"+Left(Date,2)) Case "Mar" Return(Right(Date,4)+"03"+Left(Date,2)) Case "Apr" Return(Right(Date,4)+"04"+Left(Date,2)) Case "May" Return(Right(Date,4)+"05"+Left(Date,2)) Case "Jun" Return(Right(Date,4)+"06"+Left(Date,2)) Case "Jul" Return(Right(Date,4)+"07"+Left(Date,2)) Case "Aug" Return(Right(Date,4)+"08"+Left(Date,2)) Case "Sep" Return(Right(Date,4)+"09"+Left(Date,2)) Case "Oct" Return(Right(Date,4)+"10"+Left(Date,2)) Case "Nov" Return(Right(Date,4)+"11"+Left(Date,2)) Case "Dec" Return(Right(Date,4)+"12"+Left(Date,2)) End Select End Function |
| ||
I'm french, and I use the dd-mm-yyyy format for date ^^ -> why I need speed : I sometimes attempt modifying large database with date, name content, etc... and for sure, on big database, you need every function to be the faster, if you don't want your request length less than a month :) |
| ||
I'm french, and I use the dd-mm-yyyy format for date ^^ so do we in the uk afaik its only america that uses the mm dd yyyy method (although we alternate between saying june the 18th and the 18th of june but written always dd mm yyyy) but given the date as a number working yyyymmdd works better as 20080618 is higher than 20080617 and lower than 20080619 but given the other way it becomes 17062008 18062008 19062008 (again still higher and lower than its neighbours) but instead of incrementing one digit we are working with 1 million for the day ;) and its a bit more fault tollerant on dates not within the same week month or even year |
| ||
you're right. It's just for string convinience we use the format dd mm yyyy. In any algorythm, we should prefer yyyy mm dd . |
Code Archives Forum