We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

krisdb on 07/30/07


Tagged

c


Versions (?)


dates


Published in: C# 


  1. // Formatting DateTime variables
  2. // Using DateTime methods
  3. Response.Write("Default: " + System.DateTime.Now + "<br>");
  4. Response.Write("Local Time: " + System.DateTime.Now.ToLocalTime() + "<br>");
  5. Response.Write("Long Date: " + System.DateTime.Now.ToLongDateString() + "<br>");
  6. Response.Write("Long Time: " + System.DateTime.Now.ToLongTimeString() + "<br>");
  7. Response.Write("Short Date: " + System.DateTime.Now.ToShortDateString() + "<br>");
  8. Response.Write("Short Time: " + System.DateTime.Now.ToShortTimeString() + "<br>");
  9. Response.Write("ToString Default: " + System.DateTime.Now.ToString() + "<br>");
  10. Response.Write("Universal Time: " + System.DateTime.Now.ToUniversalTime() + "<br>"); // returns a datetime, which can still be formatted
  11.  
  12. // Using Format Strings
  13. Response.Write("d - Short Date: " + System.DateTime.Now.ToString("d") + "<br>");
  14. Response.Write("D - Long Date: " + System.DateTime.Now.ToString("D") + "<br>");
  15. Response.Write("t - Short Time: " + System.DateTime.Now.ToString("t") + "<br>");
  16. Response.Write("T - Long Time: " + System.DateTime.Now.ToString("T") + "<br>");
  17. Response.Write("f - Full date/time (short time): " + System.DateTime.Now.ToString("f") + "<br>");
  18. Response.Write("F - Full date/time (long time): " + System.DateTime.Now.ToString("F") + "<br>");
  19. Response.Write("g - General: " + System.DateTime.Now.ToString("g") + "<br>");
  20. Response.Write("G - General: " + System.DateTime.Now.ToString("G") + "<br>");
  21. Response.Write("M/m - Month day: " + System.DateTime.Now.ToString("M") + "<br>");
  22. Response.Write("u - Universal Sortable: " + System.DateTime.Now.ToString("u") + "<br>");
  23. Response.Write("Y/y - Year month: " + System.DateTime.Now.ToString("y") + "<br>");
  24.  
  25. // Custom Format Strings
  26. Response.Write("d, day of month: " + System.DateTime.Now.ToString(" d") + "<br>"); // can't use just "d"
  27. Response.Write("dd, zero-padded day of month: " + System.DateTime.Now.ToString("dd") + "<br>");
  28. Response.Write("ddd, abbreviated day of week: " + System.DateTime.Now.ToString("ddd") + "<br>");
  29. Response.Write("dddd, full day of week: " + System.DateTime.Now.ToString("dddd") + "<br>");
  30.  
  31. Response.Write("h, hour: " + System.DateTime.Now.ToString(" h") + "<br>"); // can't use just "h"
  32. Response.Write("hh, hour, zero padded " + System.DateTime.Now.ToString("hh") + "<br>");
  33. Response.Write("H, hour, 24-hr " + System.DateTime.Now.ToString(" H") + "<br>"); // can't use just "H"
  34. Response.Write("H, hour, 24-hr, zero padded " + System.DateTime.Now.ToString("HH") + "<br>");
  35.  
  36. Response.Write("m, minute: " + System.DateTime.Now.ToString(" m") + "<br>"); // can't use just "m"
  37. Response.Write("M, month: " + System.DateTime.Now.ToString(" M") + "<br>"); // can't use just "M"
  38. Response.Write("MM, month, zero padded: " + System.DateTime.Now.ToString("MM") + "<br>");
  39. Response.Write("MMM, month abbreviated: " + System.DateTime.Now.ToString("MMM") + "<br>");
  40. Response.Write("MMMM, month full name: " + System.DateTime.Now.ToString("MMMM") + "<br>");
  41.  
  42. Response.Write("y, year (omits century): " + System.DateTime.Now.ToString(" y") + "<br>"); // can't use just "y"
  43. Response.Write("yy, 2-digit year, zero padded: " + System.DateTime.Now.ToString("yy") + "<br>");
  44. Response.Write("yyyy, 4-digit year, zero padded: " + System.DateTime.Now.ToString("yyyy") + "<br>");

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: krisdb on August 27, 2007

You need to login to post a comment.