Hello, I wrote a Date class to Get and Set Dates to and from ISO8601 format
It stores Year, Month, Day, Hour, Minutes, Seconds and HoursOffset (this is for hour difference from GMT=0)
Here is the code. Plop it into your own class called Date (or whatever you like)
Typical Usage:
Class Code 'Date.bas'
GetFormattedDateFor_en_GB and GetFormattedTimeFor_en_GB returns a nicely formatted date/time for England, UK (where I'm from).
My reason for not coding others is that its probably best if people from those regions make their own. Even more awesome will be if the variable names are translated to their language too.
I'd really appreciate it if you would follow the locale codes specified by android, and also add them to this thread to show off your awesomeness and for other people's benefit.
:sign0089:
It stores Year, Month, Day, Hour, Minutes, Seconds and HoursOffset (this is for hour difference from GMT=0)
Here is the code. Plop it into your own class called Date (or whatever you like)
Typical Usage:
B4X:
'Test 1
Dim Date As ClsDate
Date.Initialize()
Date.Now
ToastMessageShow ("Date test: " & Date.GetISO8601Date , True)
ToastMessageShow ("Date test: " & Date.GetFormattedDateFor_en_GB , True)
'Test 2
Date.SetDate("2013-01-01T23:12:34.123+0000")
ToastMessageShow ("Date test: " & Date.GetISO8601Date , True)
ToastMessageShow ("Date test: " & Date.GetFormattedDateFor_en_GB , True)
Class Code 'Date.bas'
B4X:
'ClsDate.bas
'Class module
'Typical Usage
' 'Test 1
' Dim Date As ClsDate
' Date.Initialize()
'
' Date.Now
'
' ToastMessageShow ("Date test: " & Date.GetISO8601Date , True)
' ToastMessageShow ("Date test: " & Date.GetFormattedDateFor_en_GB , True)
' 'Test 2
' Date.SetDate("2013-01-01T23:12:34.123+0000")
'
' ToastMessageShow ("Date test: " & Date.GetISO8601Date , True)
' ToastMessageShow ("Date test: " & Date.GetFormattedDateFor_en_GB , True)
Sub Class_Globals
Dim Year As Int = 0
Dim Month As Int = 0
Dim Day As Int = 0
Dim Hour As Int = 0
Dim Minute As Int = 0
Dim Second As Int = 0
Dim Millisecond As Int = 0
Dim HourOffset As Int = 0
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize()
End Sub
Sub Reset
Year = 0
Month = 0
Day = 0
Hour = 0
Minute = 0
Second = 0
Millisecond = 0
End Sub
Sub SetDate(StrISO8601Date As String)
Reset
If IsValidISOString(StrISO8601Date) Then
SetYear(StrISO8601Date)
SetMonth(StrISO8601Date)
SetDay(StrISO8601Date)
SetHour(StrISO8601Date)
SetMinute(StrISO8601Date)
SetSecond(StrISO8601Date)
SetMillisecond(StrISO8601Date)
SetHourOffset(StrISO8601Date)
End If
End Sub
Sub IsValidISOString(StrISO8601Date As String) As Boolean
Dim BlnResult As Boolean = False
Try
Dim CheckList As List
CheckList.Initialize
CheckList.Add(StrISO8601Date.CharAt(4)="-")
CheckList.Add(StrISO8601Date.CharAt(7)="-")
CheckList.Add(StrISO8601Date.CharAt(10)="T")
CheckList.Add(StrISO8601Date.CharAt(13)=":")
CheckList.Add(StrISO8601Date.CharAt(16)=":")
CheckList.Add(StrISO8601Date.CharAt(19)=".")
'CheckList.Add(StrISO8601Date.Length=28)
Dim BlnFalseFound As Boolean = False
For i = 0 To CheckList.Size - 1
If CheckList.Get(i) = False Then
BlnFalseFound = True
End If
Next
BlnResult = Not(BlnFalseFound)
Catch
LogColor(LastException.Message, Colors.Magenta)
End Try
Return BlnResult
End Sub
Sub SetYear(StrISO8601Date As String)
Dim StrYear As String = StrISO8601Date.SubString2(0,4)
Try
Year = StrYear
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub SetMonth(StrISO8601Date As String)
Dim StrMonth As String = StrISO8601Date.SubString2(5,7)
Try
Month = StrMonth
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub SetDay(StrISO8601Date As String)
Dim StrDay As String = StrISO8601Date.SubString2(8,10)
Try
Day = StrDay
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub SetHour(StrISO8601Date As String)
Dim StrHour As String = StrISO8601Date.SubString2(11,13)
Try
Hour = StrHour
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub SetMinute(StrISO8601Date As String)
Dim StrMinute As String = StrISO8601Date.SubString2(14,16)
Try
Minute = StrMinute
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub SetSecond(StrISO8601Date As String)
Dim StrSecond As String = StrISO8601Date.SubString2(17,19)
Try
Second = StrSecond
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub GetLengthOfMillisecondPortion(StrISO8601Date As String) As Int
Dim IntResult As Int = 0
Try
Dim BlnBreakLoop As Boolean = False
Dim IntCurrentCharacter As Int = 19
Dim ChrCurrentCharacter As Char = "."
Do While Not(BlnBreakLoop)
ChrCurrentCharacter = StrISO8601Date.CharAt(IntCurrentCharacter)
BlnBreakLoop = IntCurrentCharacter > StrISO8601Date.Length OR ChrCurrentCharacter = "+" OR ChrCurrentCharacter = "-"
If Not(BlnBreakLoop) Then
IntCurrentCharacter = IntCurrentCharacter + 1
End If
Loop
IntResult = IntCurrentCharacter - 20
Catch
LogColor(LastException.Message, Colors.Magenta)
End Try
Return IntResult
End Sub
Sub SetMillisecond(StrISO8601Date As String)
Try
Dim IntLengthOfMillisecondPortion As Int = GetLengthOfMillisecondPortion(StrISO8601Date)
Dim StrMillisecond As String = StrISO8601Date.SubString2(20,20+IntLengthOfMillisecondPortion)
Millisecond = StrMillisecond
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub SetHourOffset(StrISO8601Date As String)
Try
Dim IntLengthOfMillisecondPortion As Int = GetLengthOfMillisecondPortion(StrISO8601Date)
Dim StrHourOffset As String = StrISO8601Date.SubString2(20+IntLengthOfMillisecondPortion,25+IntLengthOfMillisecondPortion)
HourOffset = StrHourOffset
Catch
LogColor(LastException,Colors.Magenta)
End Try
End Sub
Sub GetISO8601Date As String
Dim StrResult As String = ""
Try
Dim StrISO8601Date As String = NumberFormat2(Year,4,0,0,False) & "-" & NumberFormat2(Month,2,0,0,False) & "-" & NumberFormat2(Day,2,0,0,False) & "T" & NumberFormat2(Hour,2,0,0,False) & ":" & NumberFormat2(Minute,2,0,0,False) & ":" & NumberFormat2(Second,2,0,0,False) & "." & NumberFormat2 (Millisecond,3,0,0,False)
If NumberFormat2(HourOffset,4,0,0,False) > -1 Then
StrISO8601Date = StrISO8601Date & "+" & NumberFormat2(HourOffset,4,0,0,False)
Else
StrISO8601Date = StrISO8601Date & NumberFormat2(HourOffset,4,0,0,False)
End If
StrResult = StrISO8601Date
Catch
LogColor(LastException, Colors.Magenta)
End Try
Return StrResult
End Sub
Sub IncMillisecond
Dim IntMillisecond As Int = Millisecond + 1
If IntMillisecond > 999 Then
Millisecond = 0
IncSecond
End If
Millisecond = IntMillisecond
End Sub
Sub IncMilliseconds(IntNumberOfMilliseconds As Int)
For IntIdx = 0 To IntNumberOfMilliseconds - 1
IncMillisecond
Next
End Sub
Sub IncSecond
Dim IntSecond As Int = Second + 1
If IntSecond > 59 Then
Second = 0
IncMinute
End If
Second = IntSecond
End Sub
Sub IncSeconds(IntNumberOfSeconds As Int)
For IntIdx = 0 To IntNumberOfSeconds - 1
IncSecond
Next
End Sub
Sub IncMinute
Dim IntMinute As Int = Minute + 1
If IntMinute > 59 Then
Minute = 0
IncHour
End If
Minute = IntMinute
End Sub
Sub IncMinutes(IntNumberOfMinutes As Int)
For IntIdx = 0 To IntNumberOfMinutes - 1
IncMinute
Next
End Sub
Sub IncHour
Dim IntHour As Int = Hour + 1
If IntHour > 23 Then
Hour = 0
IncDay
End If
Hour = IntHour
End Sub
Sub IncHours(IntNumberOfHours As Int)
For IntIdx = 0 To IntNumberOfHours - 1
IncHour
Next
End Sub
Sub IncDay
Dim IntDay As Int = Day + 1
If IntDay > GetDaysInMonth(Year, Month) Then
Day = 1
IncMonth
End If
Day = IntDay
End Sub
Sub IncDays(IntNumberOfDays As Int)
For IntIdx = 0 To IntNumberOfDays - 1
IncDay
Next
End Sub
Sub IncMonth
Dim IntMonth As Int = Month + 1
If IntMonth > 12 Then
Month = 1
IncYear
End If
Month = IntMonth
End Sub
Sub IncMonths(IntNumberOfMonths As Int)
For IntIdx = 0 To IntNumberOfMonths - 1
IncMonth
Next
End Sub
Sub IncYear
Year = Year + 1
End Sub
Sub IncYears(IntNumberOfYears As Int)
For IntIdx = 0 To IntNumberOfYears - 1
IncYear
Next
End Sub
Sub DecMillisecond
Dim IntMillisecond As Int = Millisecond - 1
If IntMillisecond < 0 Then
DecSecond
Millisecond = 999
End If
Millisecond = IntMillisecond
End Sub
Sub DecMilliseconds(IntNumberOfMilliseconds As Int)
For IntIdx = 0 To IntNumberOfMilliseconds - 1
DecMillisecond
Next
End Sub
Sub DecSecond
Dim IntSecond As Int = Second - 1
If IntSecond < 0 Then
DecMinute
Second = 59
End If
Second = IntSecond
End Sub
Sub DecSeconds(IntNumberOfSeconds As Int)
For IntIdx = 0 To IntNumberOfSeconds - 1
DecSecond
Next
End Sub
Sub DecMinute
Dim IntMinute As Int = Minute - 1
If IntMinute < 0 Then
DecHour
Minute = 59
End If
Minute = IntMinute
End Sub
Sub DecMinutes(IntNumberOfMinutes As Int)
For IntIdx = 0 To IntNumberOfMinutes - 1
DecMinute
Next
End Sub
Sub DecHour
Dim IntHour As Int = Hour - 1
If IntHour < 0 Then
DecDay
Hour = 23
End If
Hour = IntHour
End Sub
Sub DecHours(IntNumberOfHours As Int)
For IntIdx = 0 To IntNumberOfHours - 1
DecHour
Next
End Sub
Sub DecDay
Dim IntDay As Int = Day - 1
If IntDay < 1 Then
DecMonth
Day = GetDaysInMonth(Year, Month)
End If
Day = IntDay
End Sub
Sub DecDays(IntNumberOfDays As Int)
For IntIdx = 0 To IntNumberOfDays - 1
DecDay
Next
End Sub
Sub DecMonth
Dim IntMonth As Int = Month + 1
If IntMonth < 1 Then
DecYear
Month = 12
End If
Month = IntMonth
End Sub
Sub DecMonths(IntNumberOfMonths As Int)
For IntIdx = 0 To IntNumberOfMonths - 1
DecMonth
Next
End Sub
Sub DecYear
Year = Year - 1
End Sub
Sub DecYears(IntNumberOfYears As Int)
For IntIdx = 0 To IntNumberOfYears - 1
DecYear
Next
End Sub
Sub ReadjustVariables
Do While Millisecond < 0
IncMillisecond
Loop
Do While Millisecond > 999
DecMillisecond
Loop
Do While Second < 0
IncSecond
Loop
Do While Second > 59
DecSecond
Loop
Do While Minute < 0
IncMinute
Loop
Do While Minute > 59
DecMinute
Loop
Do While Hour < 0
IncHour
Loop
Do While Hour > 59
DecHour
Loop
Do While Day < 1
IncDay
Loop
Do While Day > GetDaysInMonth(Year,Month)
DecDay
Loop
Do While Month < 1
IncMonth
Loop
Do While Month > 12
DecMonth
Loop
End Sub
Sub IsLeapYear(IntYear As Int) As Boolean
If IntYear Mod 4 <> 0 Then Return False
If IntYear Mod 100 = 0 Then Return IntYear Mod 400 = 0
Return True
End Sub
Sub GetDaysInMonth(IntYear As Int, IntMonth As Int) As Int
Select IntMonth
Case 2: If IsLeapYear(IntYear) Then Return 29 Else Return 28
Case 4, 6, 9, 11: Return 30
Case Else: Return 31
End Select
End Sub
Sub Now
Try
Year = DateTime.GetYear(DateTime.Now)
Month = DateTime.GetMonth(DateTime.Now)
Day = DateTime.GetDayOfMonth(DateTime.Now)
Hour = DateTime.GetHour(DateTime.Now)
Minute = DateTime.GetMinute(DateTime.Now)
Second = DateTime.GetSecond(DateTime.Now)
'Millisecond = DateTime.GetM(DateTime.Now)
Catch
LogColor(LastException, Colors.Magenta)
End Try
End Sub
Sub GetFormattedDateFor_en_GB As String
Dim StrResult As String = ""
Try
Dim StrDateForLocale_en_GB As String = Day & "/" & Month & "/" & Year
StrResult = StrDateForLocale_en_GB
Catch
LogColor(LastException, Colors.Magenta)
End Try
Return StrResult
End Sub
Sub GetFormattedTimeFor_en_GB As String
Dim StrResult As String = ""
Try
Dim StrTimeForLocale_en_GB As String = (Hour + HourOffset) & ":" & Minute & ":" & Second
StrResult = StrTimeForLocale_en_GB
Catch
LogColor(LastException, Colors.Magenta)
End Try
Return StrResult
End Sub
GetFormattedDateFor_en_GB and GetFormattedTimeFor_en_GB returns a nicely formatted date/time for England, UK (where I'm from).
My reason for not coding others is that its probably best if people from those regions make their own. Even more awesome will be if the variable names are translated to their language too.
I'd really appreciate it if you would follow the locale codes specified by android, and also add them to this thread to show off your awesomeness and for other people's benefit.
:sign0089:
Last edited: