Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 29128

DateTime Formatting–the lost codes

$
0
0

Recently I’ve had a number of reasons to use Date and Time formatting.  Usually, a few educated guesses and you’ve found what you needed.  Except when you educated guess don’t get you want you expected and you do a bit of research.

For example to get a Hours, Minutes and Seconds for like 12:10:23 you could use the string HH:mm:ss.  Not so fast!  Did you really want military time (e.g. the 24 hour clock like 20:12:15)?  The capital HH gets you the 24 hour clock.

If you want the more standard 12 clock with AM and PM you’d need.  hh:mm:ss.  Additionally, you’d need the tt.  What is tt?  It the AM, PM designator.

You want milliseconds its fff.

But wait there is bit more.  There are standard and custom date/time formatting strings and some the character overlap, which explains an occasional unexpected results.

My research turned up some useful reference information.

standard date/time formatting strings (http://msdn.microsoft.com/en-us/library/az4se3k1.aspx).

For example the following get the current date.  Well for me its US local depending on your part the world it may differ.

string s = DateTime.Now.ToString(“d"); // produces current date like 8/8/2013

Custom date/time formatting strings (http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx).

For example to get the current time in the AM/PM 12 hour format use.

string s = DateTime.Now.ToString(“d"); // produces current time like 10:37:42 AM


Viewing all articles
Browse latest Browse all 29128

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>