Formatting Date & Time in a Text Field

I’m aware that the date and time formats are governed by the region and language settings (In Windows 7 anyway, not sure about MacOS).

Xojo uses these settings for display, ie. ShortDate, ShortTime etc. which makes sense, but I was wondering how easy it would be to change the format.

eg.
Dim d As New Date
MsgBox (d.LongDate + " @ " + d.ShortTime)

Which displays:

What I would like is for it to display:

The date/time is used for logging an event. I mainly want the shortened version as above because of space constraints. Also to avoid confusion with standard formats which I’m sure we all have experience with.

eg.
26/2/2017 vs 2/26/2017 (which is obvious what day it is) OR
10/2/2017 vs 2/10/2017 (which is either: 10 Feb, 2017 or 2nd Oct 2017 depending where you come from.

Fri, 10 Feb, 2017 at least provides clarity, without ambiguity, regardless of your date/time zone.

I’m hoping I can implement this without too much effort in code.

[EDIT] The time format is fine.


WIN7 Xojo2016R3

you can easily do what you want as a date format.
for example : ( works for french date only)

[code]Public Function ShortDateWithLongYear(extends aDate as Date) as String
dim res as string

res = aDate.ShortDate
res = NthField( res,"/",1)+"/"+NthField( res,"/",2)+"/"+str(aDate.Year)

Return res

End Function
[/code]

[code]Dim d As New Date
//MsgBox (d.LongDate + " @ " + d.ShortTime)

Dim dd as Integer
Dim mm as Integer
Dim yyyy as Integer
Dim mmm as String

dd = d.day
mm = d.month
yyyy = d.year

mmm = Mid(" JanFebMarAprMayJunJulAugSepOctNovDec",(mm*3)+1,3)

MsgBox (dd.ToText + " " + mmm + " " + yyyy.ToText + " @ " + d.ShortTime)[/code]

I missed the DayOfWeek in the previous code. Here I added it. I changed the variableName for Date to dt. I tested with the First Day of Week of my system clock as Sun and Mon and it seems to work okay.

[code]Dim dt As New Date
//MsgBox (d.LongDate + " @ " + d.ShortTime)

Dim d as Integer
Dim dd as Integer
Dim mm as Integer
Dim yyyy as Integer
Dim ddd as String
Dim mmm as String

d = dt.DayOfWeek
dd = dt.day
mm = dt.month
yyyy = dt.year

ddd = Mid(" SunMonTueWedThuFriSat",(d3)+1,3)
mmm = Mid(" JanFebMarAprMayJunJulAugSepOctNovDec",(mm
3)+1,3)

MsgBox (ddd + ", " + dd.ToText + " " + mmm + " " + yyyy.ToText + " @ " + dt.ShortTime)[/code]

Thanks Cho and Jean-Yves, much appreciated. I don’t make it a habit of just cutting and pasting code that I don’t understand, but I have in this case.

Cho, I did copy your original code and it didn’t show the Day Name, I was about to respond when you posted an amendment to your code.

It works PERFECTO! Thanks so very very much. :slight_smile:

[quote=318003:@Steve Kelepouris]What I would like is for it to display:

Sun, 26 Feb 2017 @ 8:12 PM[/quote]

Dim d As New Date MsgBox (d.AbbreviatedDate + " @ " + d.ShortTime)

While it worked out for you this time, I highly suggest that you be careful about this practice in the future. Many of us will answer your question with code that ILLUSTRATES the proper procedure, but the “code” may not fit your specific circumstance, or may infact be only “psuedo” code. I would invest the time to figure out what the code does, by either comparing it to information in the LR, or asking pointed questions.

On the subject of Date/Time formats… be aware that the user can CHANGE the system level formats, and if your code “assumes” it to be a particular way, it may not be so.

another way to do it

		Dim s, st as String
		Dim currentLocale As Xojo.Core.Locale
		currentLocale = Xojo.Core.Locale.Current
		Dim d As Xojo.Core.Date = Xojo.Core.Date.Now
		s = d.ToText(currentLocale,Xojo.Core.Date.FormatStyles.Full, _
		Xojo.Core.Date.FormatStyles.None)
		
		st = left(s, 3) + ", " _
		+ NthField(NthField(s, ", ", 2),". ", 1) _
		+ " " + left(NthField(NthField(s, ". ", 2)," ", 1), 3) _
		+ " " + Right(s, 4) _
		+ " @ " + d.ToText(currentLocale,Xojo.Core.Date.FormatStyles.None, _
		Xojo.Core.Date.FormatStyles.Short)
		
		MsgBox(st)

[quote=318044:@Dave S]While it worked out for you this time, I highly suggest that you be careful about this practice in the future. Many of us will answer your question with code that ILLUSTRATES the proper procedure, but the “code” may not fit your specific circumstance, or may infact be only “psuedo” code. I would invest the time to figure out what the code does, by either comparing it to information in the LR, or asking pointed questions.

On the subject of Date/Time formats… be aware that the user can CHANGE the system level formats, and if your code “assumes” it to be a particular way, it may not be so.[/quote]

Thanks for your advice Dave and to everyone’s input.

I don’t make it a habit of cut/pasting code. If I do, I check it, and work out best I can, what parts are relevant to “my” software solution.

I like the code that Cho has written, perhaps a bit verbose but that exactly suits my programming style. I do understand how it works.

eg, in general:

[quote]dt is a new instance of Date
d = dt.DayOfWeek returns an integer between 1 and 7
Using the MID function, the string “SunMonTueWedThuFriSat” is evaluated
(d*3) multiplies 3 by the DayOfWeek, and therefore counts through the string to that point
+1 moves forward the position to the first (and next) character in the string
,3 reads 3 characters from and including that position then assigns it to ddd
ddd is then displayed (plus the rest) in MsgBox
[/quote]

Is that a pass :slight_smile: (90% would be good)

[quote]On the subject of Date/Time formats…[/quote] Dave, I acknowledge this in the very first sentence in this post :).

I’ve tested Cho’s code by changing the region and language settings to the US, and even changed the start of week day. Cho’s code holds true.

d.AbbreviatedDate
does not work the same, and is too dependent on the region/language settings.

.DayOfWeek, .Day, .Month, .Year is universal and will always work, unless of course another day is added to the working week - which maybe on the cards. if so, then Cho’s code would fail.

NthField? I admit I don’t know much with that. It may well work and I apologise for not testing it, others may wish to do so.

So, if there is a flaw in Cho’s code then I would like to know why/where it is.

My thanks and apologies to Cho :slight_smile: