January 20, 2021, 01:23:58 PM

Author Topic: [bb] DateInt() by boomboom [ 1+ years ago ]  (Read 1022 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] DateInt() by boomboom [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : DateInt()
Author : boomboom
Posted : 1+ years ago

Description : 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.

Code :
Code: BlitzBasic
  1. ;Stops program running after a certain date
  2. If DateInt(CurrentDate()) >= DateInt("09 Jun 2008") End
  4. ;Stops Program running before certain date
  5. ;If DateInt(CurrentDate()) < DateInt("09 Jun 2008") End
  7. ;Program can only run on certain date
  8. ;If DateInt(CurrentDate()) <> DateInt("09 Jun 2008") End
  10. Function DateInt%(Date$)
  12.         ;Local Variables -----
  13.         Local D$
  14.         Local M$
  15.         Local Y$
  16.         Local DateResult%
  17.         ;=====================
  19.         ;Get Day
  20.         D = Left(Date,2)
  22.         ;Get Month
  23.         Select Mid$(Date,4,3)
  24.                 Case "Jan" M = "01"
  25.                 Case "Feb" M = "02"
  26.                 Case "Mar" M = "03"
  27.                 Case "Apr" M = "04"
  28.                 Case "May" M = "05"
  29.                 Case "Jun" M = "06"
  30.                 Case "Jul" M = "07"
  31.                 Case "Aug" M = "08"
  32.                 Case "Sep" M = "09"
  33.                 Case "Oct" M = "10"
  34.                 Case "Nov" M = "11"
  35.                 Case "Dec" M = "12"
  36.         End Select
  38.         ;Get Year
  39.         Y = Right(Date,4)
  41.         ;Result Date
  42.         DateResult = Int (Y + M + D)
  44.         ;Return Result Date
  45.         Return DateResult
  47. End Function

Comments :

TaskMaster(Posted 1+ years ago)

 Y=2008M=06D=09Y+M+D = 2023.???Or, it must be adding them as a string before before being converted to Int?

*(Posted 1+ years ago)

 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 :)

boomboom(Posted 1+ years ago)

 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

Ked(Posted 1+ years ago)

 Boomboom is right. Works here.

Otus(Posted 1+ years ago)

 <div class="quote">    Local D$   Local M$   Local Y$ </div>Adding strings = concatenation.

jhans0n(Posted 1+ years ago)

 Is there going to be a problem on non-English systems, or do the month abbreviations stay the same?

boomboom(Posted 1+ years ago)

 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 :)

Bobysait(Posted 1+ years ago)

Code: [Select]
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

boomboom(Posted 1+ years ago)

 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).
Code: [Select]
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

Bobysait(Posted 1+ years ago)

 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 :)

Ginger Tea(Posted 1+ years ago)

 <div class="quote"> I'm french, and I use the dd-mm-yyyy format for date ^^ </div>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 20080619but given the other way it becomes170620081806200819062008(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

Bobysait(Posted 1+ years ago)

 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 . [/i]


SimplePortal 2.3.6 © 2008-2014, SimplePortal