A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Using the DateTime objectThis tutorial shows you how to use the .NET object DateTime: how to get the current date and time, how to filter the output, check for leap years, find the number of days in a given month, add and subtract time to the current date and time. |
On Sunday, September 19th 2004 at 03:13 AM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.5 with 42 votes) |
|||||||||||
Here you'll see some examples of how to use the DateTime object.The range of DateTimeDateTime has quite a range. It starts from January 1st, year 1, hour 12:00:00 and ends in year 9999, December 31st at 11:59:59 PM. So you don't have to worry unless your application won't be updated until the year 9999 .Get the current date and timeThat's what the DateTime object is used most of the time. You can test the following code in a Console application.
Here's my output:
As expected, using DateTime.Now you'll get the current date and time of the machine on which the code runs. Using DateTime.Today you get only the current exact date, but not the time which is always set to 12:00:00. 9/18/2004 6:03:07 uses the American date format system. It's straightforward that 9 represents the month (September) and 18 is the day. Outputing current second, minute, hour...Maybe you want to greet the user depending on the hour so you only need DateTime to provide you with the current hour on the user's machine. You can easily retrieve only what interests you (current year, day of year...) with the DateTime object. The following code outputs information provided by DateTime separately:
Here's the output I got:
Ticks represent the number of ticks since January 1st, year 1, hour 12:00 (midnight). One tick equals 100 nanoseconds. Check for leap yearNo need to create a function which calculates to see if one year is a leap year because now you have IsLeapYear() which returns true if it is a leap year and false if it isn't.
2005 isn't a leap year so we'll get the answer False in the console. If you want to see if the current year is a leap year use the following combination:
I'm running this in 2004, and IsLeapYear() returns true because 2004 is indeed a leap year. How many days does this month have?You'll often need to know the number of days the current month has, and since February 'varies' this proves useful. Here's how you obtain the number of days in the current month and current year:
Adding time to the current date and timeIn other programming languages when you had to add a few hours, or even a few seconds to a date/time you had to create an entire algorithm. It wasn't simple at all. Thanks to .NET you benefit of AddHours, AddMinutes, AddSeconds etc. methods. Here's an example where seconds, minutes, days and months are added to the current date.
This is how the output looks on my computer:
Look at the result after adding 5 months to the date. Things work properly and we jump in the year 2005, February 18. Subtracting time from the current date and timeYou subtract time by adding negative values. That means you still use the methods AddHours, AddMinutes, AddSeconds etc.
Here's the result I got on this Sunday morning:
|
||||||||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||||||||||
|
||||||||||||
Current Commentssdf
Helpfull, keep up the good work.
How do find the difference in time between two dates? I'm trying to check the time that has elapsed from the date a person signed in to there next sign in.
Darryl, you could probably do it by subtracting the days of year. It\'s a property of System.Datetime. For example January 1st is day of year: 1, and febuary 1st is day of year: 32. If years are involved then just parse out the years and subtract those too.
hi,
its good.
i want to know how can we get the current time without the date.
pls help me
Hi
it is great ya
i have one doubt
if i give month in number the output will be full month name
ex:
input 9 => output September
input 7 => output July
Hi!,
Nice work.
I want to know that how will u calculate how many weeks are there in a given month.
Too good dude, this is very simple to understand and very effective for further improvement. Nice work. keep it up.
nicely done...keep it up
Hi,
Is it possible to dispose the DateTime object once I'm done using it? I'm working with limited memory so I need to free up the memory space once I'm done using the object.
Thanks in advance
Ritu
thanx you!!!!!!!!!!!!!!!!!!!!!!!!!!
that very gud but i want know the days of month
like i press 1 (means Jan ) then result will come the Jan month have 31 days. so plz do this i m waiting for your replay
sdf
Related Tutorials
Related Source Code
C# Job Search