Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Date functions
#21
Or you can use PowerShell with pipecom.
Tread on those who tread on you

Reply
#22
@SMcNeill & mdijkens, Thanks for the examples. It's really amazing what you can do with Basic.

@SMcNeill, with your program I have so far tried to "extract" what I think is important to me, namely how many days have passed between two dates. I haven't gotten very far with it yet. - I'm working on it (until the Last Judgment).  Rolleyes

@mdijkens, an error is displayed in your program that I haven't been able to solve yet. Actually, I've tried everything you can possibly try. . . it doesn't want to go away.

[Image: mdijkens-2024-03-07.jpg]
Reply
#23
For just that, I think all you'd need is: TimeStamp, CheckDayFormat, and DaysBetween.

And the Check could be removed, as long as you're certain your dates are formatted correctly.  (MM-DD-YYYY)
Reply
#24
(03-07-2024, 11:29 PM)SMcNeill Wrote: For just that, I think all you'd need is: TimeStamp, CheckDayFormat, and DaysBetween.

And the Check could be removed, as long as you're certain your dates are formatted correctly.  (MM-DD-YYYY)
Thanks! I already had two functions in mind. . . Let's see.
Reply
#25
(03-07-2024, 11:17 PM)Kernelpanic Wrote: @SMcNeill & mdijkens, Thanks for the examples. It's really amazing what you can do with Basic.

@SMcNeill, with your program I have so far tried to "extract" what I think is important to me, namely how many days have passed between two dates. I haven't gotten very far with it yet. - I'm working on it (until the Last Judgment).  Rolleyes

@mdijkens, an error is displayed in your program that I haven't been able to solve yet. Actually, I've tried everything you can possibly try. . . it doesn't want to go away.

[Image: mdijkens-2024-03-07.jpg]

I see 2 mistakes in your code:
in the first line you have to call function TIM.stamps instead of TIM.stamp if you want to provide your date as a string
Furthermore the date string you provide is a parameter and should be between brackets ("your date"):

Code: (Select All)
date1 = TIM.stamps("1917-09-24 15:52:31") 'seconds since 01-01-0001
And did you include the library in your code? I don't see it in the screenshot
Code: (Select All)
Function TIM.stamps~&& (dt$)
  year% = Val(Left$(dt$, 4)): month% = Val(Mid$(dt$, 6, 2)): day% = Val(Mid$(dt$, 9, 2))
  hour% = Val(Mid$(dt$, 12, 2)): minute% = Val(Mid$(dt$, 15, 2)): second% = Val(Mid$(dt$, 18, 2))
  TIM.stamps~&& = TIM.stamp(year%, month%, day%, hour%, minute%, second%)
End Function

Function TIM.string$ (dt~&&)
  dt$ = TIM.dateTime$(dt~&&, year%, month%, day%, hour%, minute%, second%)
  TIM.string$ = Left$(dt$, 4) + "-" + Mid$(dt$, 5, 2) + "-" + Mid$(dt$, 7, 2) + " " + _
    Mid$(dt$, 9, 2) + ":" + Mid$(dt$, 11, 2) + ":" + Mid$(dt$, 13, 2)
End Function

Function TIM.now~&& ()
  dat$ = Date$: tim~& = Timer
  month% = Val(Left$(dat$, 2))
  day% = Val(Mid$(dat$, 4, 2))
  year% = Val(Mid$(dat$, 7, 4))
  TIM.now~&& = TIM.days~&(year%, month%, day%) * 86400~&& + tim~&
End Function

Function TIM.utc~&& ()
  Type UTCtype
    year As Integer
    month As Integer
    weekday As Integer
    day As Integer
    hour As Integer
    minute As Integer
    second As Integer
    millis As Integer
  End Type
  Declare Dynamic Library "Kernel32"
    Sub GetUTC Alias GetSystemTime (lpSystemTime As UTCtype)
  End Declare
  Dim utc As UTCtype: GetUTC utc
  TIM.utc~&& = TIM.stamp~&&(utc.year, utc.month, utc.day, utc.hour, utc.minute, utc.second)
End Function

Function TIM.stamp~&& (year%, month%, day%, hour%, minute%, second%)
  TIM.stamp~&& = TIM.days~&(year%, month%, day%) * 86400~&& + TIM.seconds~&(hour%, minute%, second%)
End Function

Function TIM.days~& (year%, month%, day%)
  leap% = TIM.leapYear(year%): prevYear% = year% - 1
  dPrevYears& = ((((prevYear% * 365) + (prevYear% \ 4)) - (prevYear% \ 100)) + (prevYear% \ 400))
  Select Case month%
    Case 1: TIM.days~& = ((dPrevYears&) + day%) - 1
    Case 2: TIM.days~& = ((dPrevYears& + 31) + day%) - 1
    Case 3: TIM.days~& = ((dPrevYears& + 59 - leap%) + day%) - 1
    Case 4: TIM.days~& = ((dPrevYears& + 90 - leap%) + day%) - 1
    Case 5: TIM.days~& = ((dPrevYears& + 120 - leap%) + day%) - 1
    Case 6: TIM.days~& = ((dPrevYears& + 151 - leap%) + day%) - 1
    Case 7: TIM.days~& = ((dPrevYears& + 181 - leap%) + day%) - 1
    Case 8: TIM.days~& = ((dPrevYears& + 212 - leap%) + day%) - 1
    Case 9: TIM.days~& = ((dPrevYears& + 243 - leap%) + day%) - 1
    Case 10: TIM.days~& = ((dPrevYears& + 273 - leap%) + day%) - 1
    Case 11: TIM.days~& = ((dPrevYears& + 304 - leap%) + day%) - 1
    Case 12: TIM.days~& = ((dPrevYears& + 334 - leap%) + day%) - 1
    Case Else: TIM.days~& = 0
  End Select
End Function

Function TIM.seconds~& (hour%, minute%, second%)
  TIM.seconds~& = hour% * 3600 + minute% * 60 + second%
End Function

Function TIM.dateTime$ (timestmp~&&, year%, month%, day%, hour%, minute%, second%)
  tdays~& = timestmp~&& \ 86400 + 306
  secs~& = timestmp~&& Mod 86400
  era% = tdays~& \ 146097
  doe~& = tdays~& Mod 146097 ' [0, 146096]
  yoe% = (doe~& - doe~& \ 1460 + doe~& \ 36524 - doe~& \ 146096) \ 365 ' [0, 399]
  year% = yoe% + era% * 400
  doy% = doe~& - (365 * yoe% + yoe% \ 4 - yoe% \ 100) ' [0, 365]
  mp% = (5 * doy% + 2) \ 153 ' [0, 11]
  day% = doy% - (153 * mp% + 2) \ 5 + 1 ' [1, 31]
  If mp% < 10 Then month% = mp% + 3 Else month% = mp% - 9 ' [1, 12]
  year% = year% - (month% <= 2)
  dat$ = Right$(Str$(year% + 10000), 4) + Right$(Str$(month% + 100), 2) + Right$(Str$(day% + 100), 2)
  hour% = secs~& \ 3600
  minsec% = secs~& - (hour% * 3600)
  minute% = minsec% \ 60
  second% = minsec% - (minute% * 60)
  TIM.dateTime$ = dat$ + Right$(Str$(hour% + 100), 2) + _
    Right$(Str$(minute% + 100), 2) + Right$(Str$(second% + 100), 2)
End Function

Function TIM.format$ (ts~&&)
  dt$ = TIM.dateTime$(ts~&&, year, month, day, hour, minute, second)
  dt2$ = Mid$("SuMoTuWeThFrSa", TIM.weekDay(ts~&&) * 2 + 1, 2)+" " + _
    Mid$(dt$, 7, 2) + "-" + Mid$(dt$, 5, 2) + "-" + Mid$(dt$, 1, 4) + " " + _
    Mid$(dt$, 9, 2) + ":" + Mid$(dt$, 11, 2) + ":" + Mid$(dt$, 13, 2)
  TIM.format$ = dt2$
End Function

Function TIM.leapYear% (year%)
  If (year% Mod 4) <> 0 Then
    TIM.leapYear% = 0
  ElseIf (year% Mod 100) = 0 Then
    TIM.leapYear% = (year% Mod 400) = 0
  Else
    TIM.leapYear% = Not 0
  End If
End Function

Function TIM.weekDay% (ts~&&)
  tdays~& = ts~&& \ 86400
  TIM.weekDay% = (tdays~& + 1) Mod 7
End Function

Function TIM.dst& (ts~&&, timezone%)
  dt$ = TIM.dateTime$(ts~&&, year%, month%, day%, hour%, minute%, second%)
  summer~&& = TIM.stamps(Right$(Str$(year% + 10000), 4) + "-03-31 01:00:00") 'UTC
  summer~&& = summer~&& - (TIM.weekDay(summer~&&) * 86400)
  winter~&& = TIM.stamps(Right$(Str$(year% + 10000), 4) + "-10-31 01:00:00") 'UTC
  winter~&& = winter~&& - (TIM.weekDay(winter~&&) * 86400)
  If ts~&& >= summer~&& And ts~&& < winter~&& Then
    TIM.dst = timezone% * 3600 + 3600 ' UTC + timezone% + dst (summer)
  Else
    TIM.dst = timezone% * 3600 ' ' UTC + timezone% (winter)
  End If
End Function
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#26
Here's something I wrote 4 years ago:
Date Picker: Finished (alephc.xyz)

It has a difference between days calculation and a way to return the weekday for a given date.
Tread on those who tread on you

Reply
#27
@SpriggsySpriggs, now I know where the error was, the source code posted by @mdijkens was only part of the entire program. 

It works now.
Reply
#28
(03-08-2024, 03:20 PM)Kernelpanic Wrote: @SpriggsySpriggs, now I know where the error was, the source code posted by @mdijkens was only part of the entire program. 

It works now.

Happy you got it working now. I hope you'll see how easy it is to use  Smile
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply
#29
Quite the competition for who gets the best dates! ;-))
b = b + ...
Reply
#30
I think all are good, just depends on need and style  Smile
45y and 2M lines of MBASIC>BASICA>QBASIC>QBX>QB64 experience
Reply




Users browsing this thread: 2 Guest(s)