Anda di halaman 1dari 6

Dates and Times in C# .

NET
At some stage of your programming career, you'll need the ability to manipulate dates 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 Date ime. Add this to you button code! DateTime theDate; theDate = DateTime.Now; MessageBox.Show( theDate.ToString() ); After setting up a Date ime "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 Date ime 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

theDate = DateTime.Ut Now; $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 when you type the dot after Date ime!

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

o use this, then, you can set up a new integer "ariable and hand it the day! theDate = DateTime.Ut Now; 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.Ut Now; 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 for yourself!

Another thing you can do with Date ime 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 Date ime 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 of 8eros!

,f you want to calculate the difference between one date and another, then a more precise structure is used with Date ime 9 imeSpan. 2ere's how to use it! DateTime -irstDate = new DateTime(,''(. '$. $%); DateTime se ondDate = DateTime./arse(#$ 0e1 ,''(#); TimeS*an dateDi--; dateDi-- = se ondDate.S"1tra t(-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 a different way! DateTime se ondDate = DateTime./arse(#$ 0e1 ,''(#); After the dot of Date ime, 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 imeSpan structure. his uses similar methods and properties as Date ime. One of the options is Subtract! TimeS*an dateDi--; dateDi-- = se ondDate.S"1tra t( -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! Add.onths, 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 course has lots more lessons and tutorials. ,f you want to buy it, click a link at the top of these pages.

Anda mungkin juga menyukai