Managing Date Format

I want date to always appear in format ##/##/#### (e.g. “01/01/1950”)

Is there a better way to do this that what I show below?

Dim d As New Date
Dim L As Boolean

Dim s As String = "1/1/1950"

L = ParseDate(s,d)

Dim nMonth As Integer = d.Month
Dim nDay As Integer = d.Day 
Dim nYear As Integer = d.Year

Dim cMonth As String = Format(nMonth, "0#")
Dim cDay As String = Format(nDay, "0#")
Dim cYear As String = Format(nYear, "####")

Dim Date As String = cMonth + "/" + cDay + "/" + cYear

If L =  True Then
  TextField1.Text = Date
Else
  TextField1.Text = "Error"
End If

Not exactly ‘better’, just less code:

[code]Dim d As New Date
Dim s As String = “1/1/1950”

If ParseDate(s,d) Then
TextField1.Text = Format(d.Month, “0#”) + “/” + Format(d.Day, “0#”) + “/” + d.Year.ToText
Else
TextField1.Text = “Error”
End If[/code]

If you want to use Left, Mid, Right, instead of Format, you can change that line of code to:

TextField1.Text = d.SQLDate.Mid(6,2) + "/" + d.SQLDate.Right(2) + "/" + d.SQLDate.Left(4)

Less code is good. Thanks Alberto.

how do you deal with 01/02/1950 ?
is it the 1st of february, or the 2nd of january ?

[quote=439562:@Jean-Yves Pochez]how do you deal with 01/02/1950 ?
is it the 1st of february, or the 2nd of january ?[/quote]
Yes. There’s nothing more irritating than a date presented that way with the presenter presuming to define what it means without telling anyone. Use Jan/Feb etc for the month.

I’m not sure I follow. I suppose if your in Europe you might expect 01/02/1950 = February 1st, whereas if you are in the U.S. you might expect 01/02/1950 = January 2nd. But, I’m curious what you are suggesting?

The complete opposite of what you set out to implement. If you’re happy with how it’s functioning, you’re all set.

For the sake of argument lets represent one date Jan 5, 2019 in several styles

YYYY is 4 digit year
MMM is 3 letter month abbreviation
MM is 2 digit month
DDis 2 digit day

YYYY-MMM-DD 2019 Jan 5
MMM-DD, YYYY Jan 5, 2019
YYYY-MM-DD 2019-01-05 (note this could also be May 1, 2019 as YYYY-DD-MM but thats unlikely)
MM-DD-YYYY 01-05-2019 (note this could also be May 1, 2019 as DD-MM-YYYY and thats very likely)

The three letter month names are pretty unambiguous to anyone using a gregorian calendar (even if you write them as DD-MMM-YYYY or just about any other form) You cant mistake a month for a day and vice versa

Sure. Can’t argue with that. I don’t know how it is elsewhere, but I think everyone in the US knows 01/05/2019 = January 5, 2019. And that is the format you see here EVERYWHERE! Not necessarily exclusively, but it’s everywhere. But you are right, if one is building an app for international use, then by all means remove any ambiguity.

I’m Canadian and while we normally write them one way we get enough things from the USA that are different it gets darned confusing after a while.
Payroll system I worked on for a large Canadian company used YYYYMMDD internally but everything for display was one of the two formats with three letter months just so it was not ambiguous to our Canadian or US employees.

that parsedate method is internationnal-savvy, and what you suppose with your date must be aware that it will perhaps behave different in other part of the world. when you first posted, you didnt mention your location, so it could be of importance.
just my 2cts, no big deal.

Month then day is as American as Apple Pie and the 4th of July… opps.

Everyone knows that 911 happened on the 9th of November.

Navel gazing…

Or developer syndrome (everyone has the same configuration as me).

Nothing apart imperial measures exists… (even 1776 does not existsed [imperial measures]) :frowning: