Date & Time Libs

pixelpop

Active Member
Licensed User
Longtime User
Is there a lib that will easily convert "2013-02-05" to "Tuesday, February 5, 2013" and "15:30:00" to "3:30 PM"? Thanks.
 

mangojack

Expert
Licensed User
Longtime User
To work with current date u can use this ..
B4X:
Dim now As Long   
now = DateTime.now
   
DateTime.DateFormat = "EEEE, MMMM d, yyy"   
Msgbox(DateTime.Date(now),"")

DateTime.DateFormat = "hh:mm a"
Msgbox(DateTime.Date(now),"")

for formatting parameters see here ...
Date Formatting

Cheers mj
 
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Hey.... helloooooo...

Don't forget my little contribution :)

:sign0148:

If you put your subroutines into a separate code module, call it say MySubs then when you are in another part of your project and you want to use one you type:

MySubs. then when you type the dot you get intellisense!

B4X:
Sub dayName(dt As Long) As String
   Dim dayNames() As String:dayNames = Array As String("","Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
   Return dayNames(DateTime.GetDayOfWeek(dt))
End Sub

Sub monthName(dt As Long) As String
   Dim monthNames() As String:monthNames = Array As String("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
   Return monthNames(DateTime.GetMonth(dt))
End Sub

Sub ampmTime(dt As Long) As String
   Dim h As Int
    Dim i As Double
   i = DateTime.GetMinute(dt)
   h = DateTime.GetHour(dt)
   If h <= 11 Then 
      If h = 0 Then h = 12 'coz we can't have hours as zero :)
      Return h & ":" & NumberFormat(i, 2, 0) & "am"
   End If
   h = h - 12
   If h = 0 Then h = 12 'coz we can't have hours as zero :)
   Return h & ":" & NumberFormat(i, 2, 0) & "pm"
End Sub

Sub daySuffix(i As Int) As String 'add suffix to integer such as 10th 3rd 1st 2022nd etc.
   Dim s As String
   Dim j As Int
   s = i 'change the int to a string
   j = i 'make a copy of i that we can chop up
   If s.Length > 2 Then s = s.SubString(s.Length - 2)
   j = s 'j is now a number that is no bigger than 2 digits
   If j >= 4 AND j <= 20 Then Return i & "th" 'these last few
   If s.EndsWith("1") Then Return i & "st"    'lines work out
   If s.EndsWith("2") Then Return i & "nd"    'the correct suffix
   If s.EndsWith("3") Then Return i & "rd"    'and send back the 
   Return i & "th" 'original number as a string with the correct suffix after it.
End Sub

Sub longDate(dt As Long) As String
   Return dayName(dt) & " " & daySuffix(DateTime.GetDayOfMonth(dt)) & " of " & monthName(dt) & ", " & DateTime.GetYear(dt)     
End Sub

Sub ampmTimeAndlongDate(dt As Long) As String
   Return ampmTime(dt) & " on " & longDate(dt)  
End Sub

I include a little example project.

And see the separate thread with more info here:
http://www.b4x.com/forum/basic4android-updates-questions/25460-datetime-bloomin-complicated.html
 

Attachments

  • DateTimesTest.zip
    8.2 KB · Views: 215
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
To work with current date u can use this ..
B4X:
Dim now As Long   
now = DateTime.now
   
DateTime.DateFormat = "EEEE, MMMM d, yyy"   
Msgbox(DateTime.Date(now),"")

DateTime.DateFormat = "hh:mm a"
Msgbox(DateTime.Date(now),"")

for formatting parameters see here ...
Date Formatting

Cheers mj

The issue is that I am trying to convert a date in string format, not just a "Now" variable. So if my date is the string "2013-02-06", to use the above method I would need to convert that date string to ticks. How would I do that or is there another method for making this conversion?
 
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
Thanks, Erel. I tried this code, and while it passes compile, the DateTime.GetDayOfWeek function returns a number (in the case below, I get a "4" which equates to Wednesday).

B4X:
DateTime.DateFormat = "yyyy-MM-dd"
chkstr = DateTime.GetDayOfWeek(DateTime.DateParse("2013-02-06"))

I could use a map to get the day name, but I have a hunch there is a more elegant way (like Visual Basic).
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can use something simple like this:
B4X:
DateTime.DateFormat = "yyyy-MM-dd"
Dim chkstr As Int
Dim MyDays() As String
MyDays=Array As String("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
chkstr = DateTime.GetDayOfWeek(DateTime.DateParse("2013-02-06"))
Msgbox(MyDays(chkstr-1),"") 'will return Wednesday
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
Here's one way to do it:

B4X:
DateTime.DateFormat = "yyyy-MM-dd"
Dim t As Long = DateTime.DateParse("2013-02-06")
DateTime.DateFormat = "EEEE"   ' EEEE text day of week
Dim chkstr As String = DateTime.Date(t)
 
Last edited:
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
Here's one way to do it:

B4X:
DateTime.DateFormat = "yyyy-MM-dd"
Dim t As Long = DateTime.DateParse("2013-02-06")
DateTime.DateFormat = "EEEE"   ' EEEE text day of week
Dim chkstr As String = DateTime.Date(t)

MLDev, that's exactly what I was looking for! Taking your lead, I used:

B4X:
DateTime.DateFormat = "yyyy-MM-dd"
Dim t As Long = DateTime.DateParse("2013-02-06")
DateTime.DateFormat = "EEEE, MMMM d, yyyy"    ' EEEE text day of week
Dim chkstr As String = DateTime.Date(t)

that returned exactly what I was looking for: "Wednesday, February 6, 2013"! Big thanks!
 
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
By the way, this is completely off-topic, but is there a way to copy code in forum posts to B4A so that the code retains formatting (CRLFs)?
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
FYI You can also add time text with DateTime.DateFormat:

B4X:
DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
Dim t As Long = DateTime.DateParse("2013-02-06 15:30:00")
DateTime.DateFormat = "EEEE, MMMM d, yyyy, h:mm aa"   ' EEEE text day of week
Dim chkstr As String = DateTime.Date(t)   ' chkstr = Wednesday, February 6, 2013, 3:30 PM

By the way, this is completely off-topic, but is there a way to copy code in forum posts to B4A so that the code retains formatting (CRLFs)?

I know. It's a pain in the ass to copy and paste then go back and format the code. I've even tried copying it into Notepad++ and you still have to format it.
 
Last edited:
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
Thanks, MLDev. Once you opened the door to me on how to get the formatting and translation done, everything just fell into place. The date and time are going into two different labels, but the process is the same. :icon_clap:
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Originally Posted by pixelpop View Post
By the way, this is completely off-topic, but is there a way to copy code in forum posts to B4A so that the code retains formatting (CRLFs)?

I know. It's a pain in the ass to copy and paste then go back and format the code. I've even tried copying it into Notepad++ and you still have to format it.

Again .. Off topic but
Slightly confused .. I frequently select/copy/past code directly into B4a without reformatting, maybe a tidy up (tabbing,indents etc.). Am I missing something ?

Cheers mj
 
Upvote 0

pixelpop

Active Member
Licensed User
Longtime User
Are you just doing a highlight, Cntrl-C (or right mouse, Copy), then pasting into B4A? When I (and I assume others) do that, the copied lines of code are pasted with returns removed, resulting in one long line of code that has to be manually broken down into multiple lines again.
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Are you just doing a highlight, Cntrl-C (or right mouse, Copy), then pasting into B4A? When I (and I assume others) do that, the copied lines of code are pasted with returns removed, resulting in one long line of code that has to be manually broken down into multiple lines again.

Normally I highlight code and right click mouse to use context menu to copy and paste, but I just tried CTRL-C .. CTRL-V and the result was still good. ?
Win7 and Firefox.

Cheers mj
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
Again .. Off topic but
Slightly confused .. I frequently select/copy/past code directly into B4a without reformatting, maybe a tidy up (tabbing,indents etc.). Am I missing something ?

Cheers mj

When I select code from a code box on the forum and paste it into the IDE it's just one line with no line breaks. Can you copy from a code box and paste it without reformatting? If so I would like to know how you're doing it. :confused:
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
Normally I highlight code and right click mouse to use context menu to copy and paste, but I just tried CTRL-C .. CTRL-V and the result was still good. ?
Win7 and Firefox.

Cheers mj

LOL I keep saying I should quit using IE. I just tried it with Firefox and the format was correct. Bye bye IE.
 
Upvote 0
Top