6
Dates and Times in C# .NET At some stage of your programming career, you'll need the ability to manipulate d ates and time. A typical example would be a database programme where you want to record when an entry was made, especially if it's an order for a product. Or if you want to, say, calculate how many days it's been since an order was placed. Start a new project for this. Add a button to your new form, and double click it to get at the code. An inbuilt structure you can use to manipulate dates is called Dateime. Add this to you  button code! DateTime theDate; theDate = DateTime.Now; MessageBox.Show( theDate.ToString() ); After setting up a Dateime "ariable called theDate, we ha"e this line! theDate = DateTime.Now; he Now property returns the current system date and time of your computer, where"er you are in the world. he third line in our code con"erts the Dateime into a string and then displays it in a message box. #hen the code is run, and the button clicked, the message box will be something like this! his is the date in $% format &day first, then month, followed by the current time &hours, minutes and seconds. (ou can also ha"e this, instead of )ow! theDate = DateTime.Today; And e"en this *

15 C# .NET Dates and Times

Embed Size (px)

Citation preview

8/13/2019 15 C# .NET Dates and Times

http://slidepdf.com/reader/full/15-c-net-dates-and-times 1/6

Dates and Times in C# .NET

At some stage of your programming career, you'll need the ability to manipulate datesand time. A typical example would be a database programme where you want to record

when an entry was made, especially if it's an order for a product. Or if you want to, say,

calculate how many days it's been since an order was placed.

Start a new project for this. Add a button to your new form, and double click it to get atthe code.

An inbuilt structure you can use to manipulate dates is called Dateime. Add this to you

 button code!

DateTime theDate;

theDate = DateTime.Now;

MessageBox.Show( theDate.ToString() );

After setting up a Dateime "ariable called theDate, we ha"e this line!

theDate = DateTime.Now;

he Now property returns the current system date and time of your computer, where"er

you are in the world.

he third line in our code con"erts the Dateime into a string and then displays it in amessage box. #hen the code is run, and the button clicked, the message box will be

something like this!

his is the date in $% format &day first, then month, followed by the current time &hours,

minutes and seconds.

(ou can also ha"e this, instead of )ow!

theDate = DateTime.Today;

And e"en this

*

8/13/2019 15 C# .NET Dates and Times

http://slidepdf.com/reader/full/15-c-net-dates-and-times 2/6

theDate = DateTime.UtNow;

$tc is something called +oordinated $ni"ersal ime, or nternational Atomic ime.

ry all three in your code and see how they differ.

o get at just the year, or the month, or the day, take a look at the ntelliSense list whenyou type the dot after Dateime!

f you click on Day to highlight it, you'll see that it is an nteger!

-

8/13/2019 15 C# .NET Dates and Times

http://slidepdf.com/reader/full/15-c-net-dates-and-times 3/6

o use this, then, you can set up a new integer "ariable and hand it the day!

theDate = DateTime.UtNow;

int theDay = theDate.Day;

he onth and (ear are also ntegers, so the code is similar!

int theMonth = theDate.Month;

Or 

int the!ear = theDate.!ear;

(ou can also con"ert your date to a string, and use something called a /ormat 0ro"ider.

ry this code!

DateTime theDate = DateTime.UtNow;

string "stom = theDate.ToString(d);

MessageBox.Show("stom);

#hen you run your programme and click the button, the date displays in this format!

$%&'%&'

+hange the 1d1 in the code abo"e to 1D1 &capital D instead of lowercase. #hen you run

the code, the date is displayed like this!

$% *ri+ ,''

2ere's a list of letters you can use, and what they will display. ry a few and see foryourself!

3

8/13/2019 15 C# .NET Dates and Times

http://slidepdf.com/reader/full/15-c-net-dates-and-times 4/6

Another thing you can do with Dateime is to specify a format. ype the following!

DateTime -irstDate = new DateTime

After the final 1e1, type a round bracket. (ou should see this!

here are *- different ways to use the Dateime structure. 4ach one is gi"ing you the

option of a date5time format. 4xamine option 6 of *-!

(ou can just type some numbers here!

DateTime -irstDate = new DateTime(,'' '$ $%);

#hat you'll get back is the Date you specified. 7ut you'll also get the time as a series of8eros!

6

8/13/2019 15 C# .NET Dates and Times

http://slidepdf.com/reader/full/15-c-net-dates-and-times 5/6

f you want to calculate the difference between one date and another, then a more precise

structure is used with Dateime 9 imeSpan. 2ere's how to use it!

DateTime -irstDate = new DateTime(,'' '$ $%);

DateTime seondDate = DateTime./arse($ 0e1 ,'');

TimeS*an dateDi--;

dateDi-- = seondDate.S"1trat(-irstDate);

MessageBox.Show(Date di--2 3 dateDi--.ToString() );

he first line sets up a date! the *6th of :anuary -;;<. he second line sets up a date in adifferent way!

DateTime seondDate = DateTime./arse($ 0e1 ,'');

After the dot of Dateime, we're using /arse. n between the round brackets of 0arse,

simply type your date between double =uotes.

#hen you subtract one date from another, the answer is returned as a imeSpanstructure. his uses similar methods and properties as Dateime. One of the options is

Subtract!

TimeS*an dateDi--;

dateDi-- = seondDate.S"1trat( -irstDate );

After typing a date then a dot, use the Subtract method. n between the round brackets of

Subtract, you need another date 9 the one you're trying to subtract. #hen the code is run,

it will calculate the difference between the two dates.

(ou can also add date and time "alues to ones you already ha"e. 4xamine this code!

-irstDate = -irstDate.ddDays(4');

MessageBox.Show( -irstDate.ToString(D) );

 )ow we're using AddDays after our firstDate "ariable. n between the round brackets of

AddDays, type how many days you want to add.

#hen the ntelliSense list appears, ha"e a look at other date and time "alues you can add!

Addonths, Add(ears, etc.

7ut that's it for this beginners course on >isual +? .)4. #e hope you enjoyed it, and

wish you all the best for your future programming career. he book "ersion of this coursehas lots more lessons and tutorials. f you want to buy it, click a link at the top of these

 pages.

@

8/13/2019 15 C# .NET Dates and Times

http://slidepdf.com/reader/full/15-c-net-dates-and-times 6/6