DateTime Flashcards
How to create date time?
var dateTime = new DateTime()
How to get current date and time?
DateTime.Now;
Now is a static property
How to get todays? date?
DateTime.Today;
Tell about a few Date Time methods?
now.AddHout()
Methods starts with Add
DateTime objects are immutable
How to convert date and time to string?
now. toLongDateString()
now. toShortDateString()
now. toLongTImeString()
now. toShortTimeString()
now. ToString()
ToSTring also takes format specifiers
How to create TImeSpan object?
var timeSpan = new TimeSpan()
timeSpan.FromHours(1)
How to convert TimeSpan to string>
TimeSpan.ToString
How to convert string to timeSpan.
TimeSpan.Parse()
What will be the output of this program?
var dateTime = new DateTime(2015, 1, 1);
dateTime.AddYears(1);
Console.WriteLine(dateTime.Year);
2015
Is it not 2016 because DateTime objects are immutable, which means once they’re set, they cannot be changed. Here, AddYears() method returns a new DateTime object but we’re not storing it in any variables
How can we get the current year?
DateTime.Now returns a DateTime object that represents the current date/time. This object has a Year property that we can access to get the current year.