DateTime.ToString

When you want to format a date you have two choices.
(1) Use a built-in standard date format
(2) Use a custom date format


To format a date tou can use the ToString method

System.DateTime oDateTime; 
oDateTime = System.DateTime.UtcNow;
System.Diagnostics.Debug.Print(oDateTime.ToString("dd-mm-yyyy"));

Instead of always using a custom date format there are also some built-in standard formats
These standard formats are only one character long.
If the format provided has more than one character it is assumed to be a custom format.

System.Diagnostics.Debug.Print(oDateTime.ToString("d")); 

dShort Date
DLong Date
fFull Date (Long Date Short Date)
FFull Date (Long Date Long Time)
gGeneral Date (Short Date, Short Time)
GGeneral Date (Short Date, Long Time)
m, MMonth Day
o, O 
r, R 
s 
tShort Time
TLong Time
uUniversal (Short Date Short Time)
UUniversal (Long Date Long Time)
y, Y 

System.Diagnostics.Debug.Print(oDateTime.ToString("d", new CultureInfo.CurrentCulture)); 
System.Diagnostics.Debug.Print(oDateTime.ToString("d", new CultureInfo("en-US"));
System.Diagnostics.Debug.Print(oDateTime.ToString("d", new CultureInfo("en-GB"));


The easiest way to obtain a date/time in a a specific format is to use the ToString method

oDateTime.ToString("dddd, dd mmm yy") = "Monday, 18 Jul 06" 

oDateTime.ToString("m") = ?? 

You must use "MM" and not "mm" when converting a DateTime to a specific text format

oDataRow.Item("Birthday") = Me.dtpClientBirthday.Value.ToString("dd/MM/yyyy") 

String to DateTime
String to TimeSpan



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext