DateTime Flashcards

1
Q

How to create date time?

A

var dateTime = new DateTime()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to get current date and time?

A

DateTime.Now;

Now is a static property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to get todays? date?

A

DateTime.Today;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Tell about a few Date Time methods?

A

now.AddHout()

Methods starts with Add

DateTime objects are immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to convert date and time to string?

A

now. toLongDateString()
now. toShortDateString()
now. toLongTImeString()
now. toShortTimeString()
now. ToString()

ToSTring also takes format specifiers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to create TImeSpan object?

A

var timeSpan = new TimeSpan()

timeSpan.FromHours(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to convert TimeSpan to string>

A

TimeSpan.ToString

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to convert string to timeSpan.

A

TimeSpan.Parse()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will be the output of this program?

var dateTime = new DateTime(2015, 1, 1);

dateTime.AddYears(1);

Console.WriteLine(dateTime.Year);

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can we get the current year?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly