.NET - C# - DateTime - Basics


/ Published in: C#
Save to your folder(s)

Basic examples on the usage of the DateTime objects.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.