Posted By

denakitan on 10/20/12


Tagged

Net DateTime formatting basics examples c# operations


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Priestd09
danfinrud55


.NET - C# - DateTime - Basics


 / Published in: C#
 

Basic examples on the usage of the DateTime objects.

  1. // outputting today's date formatted as dd/mm/yyyy
  2. Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy"));
  3.  
  4. // creating a DateTime object
  5. DateTime miyazakiBirthDate = new DateTime(1941, 1, 5);
  6. Console.WriteLine(miyazakiBirthDate.ToString("dd/MM/yyyy"));
  7.  
  8. // example of DateTime arithmethic operation
  9. TimeSpan oneDay = new TimeSpan(1, 0, 0, 0);
  10. DateTime miyazakiBirthNextDay = miyazakiBirthDate + oneDay;
  11. Console.WriteLine(miyazakiBirthNextDay.ToString("dd/MM/yyyy"));
  12.  
  13. // another way to output a formatted DateTime
  14. Console.WriteLine(string.Format("{0:MM/dd/yyy}", miyazakiBirthDate));

Report this snippet  

You need to login to post a comment.