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"));
d | Short Date |
D | Long Date |
f | Full Date (Long Date Short Date) |
F | Full Date (Long Date Long Time) |
g | General Date (Short Date, Short Time) |
G | General Date (Short Date, Long Time) |
m, M | Month Day |
o, O | |
r, R | |
s | |
t | Short Time |
T | Long Time |
u | Universal (Short Date Short Time) |
U | Universal (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
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext