using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 16, 8, 44);
string strTime = time.ToString();
Console.WriteLine("Date and Time(DateTime): {0}\n", time);
Console.WriteLine("Date and Time(String): {0}\n", strTime);
return 0;
}
}
}
This would produce:
Date and Time(DateTime): 4/22/2002 4:08:44 PM
Date and Time(String): 4/22/2002 4:08:44 PM
Press any key to continue . . .
Notice that the value produced by the string includes both the date, the time
and even the AM/PM section. In some cases, you would be interested only in
either the date or the time. To support this type of operation, the DateTime
structure has another overloaded version of the ToString() method that
takes as argument a String value. Its syntax is:
public string ToString(string format);
When calling this method, there are rules you should/must follow.
Like dates, time values follow the Regional (and Language) Settings of Control Panel when they display. To make this display friendlier, Microsoft Windows provides
the rules, through some characters, you can use to format a time:
The characters used to create a format are:
Format |
Used For |
Description |
hh |
Hours |
An hour number from 0 to 23.
If the hour is less than 10, it would display with the leading 0 such as 08.
Here is an example:using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 5, 8, 37);
string strHour = time.ToString("hh");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Hour Value: {0}\n", strHour);
return 0;
}
}
}
This would produce:Date and Time: 4/22/2002 5:08:37 AM
Hour Value: 05
Press any key to continue . . .
If the hour is greater than 12, such as 15, 12 would be subtract
from it and it would display with a leading 0, such as 05. Here is
an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
string strHour = time.ToString("hh");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Hour Value: {0}\n", strHour);
return 0;
}
}
}
This would produce:Date and Time: 4/22/2002 5:08:37 AM
Hour Value: 05
Press any key to continue . . .
|
HH |
Hours |
An hour number from 0 to 23.
If the hour is less than 10, it would display with the leading 0 such as 08.
The effect is the same as seen for the hh format.
If the hour is greater than 12, such as 15, it would display with
that value. Here is an example:using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
string strHour = time.ToString("HH");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Hour Value: {0}\n", strHour);
return 0;
}
}
}
This would produce:Date and Time: 4/22/2002 3:08:37 PM
Hour Value: 15
Press any key to continue . . .
|
mm |
Minutes |
A minute number from 0 to 59.
If the number is less than 10, it would display with the leading 0 such as 06.
Here is an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
string strMinute = time.ToString("mm");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Minute Value: {0}\n", strMinute);
return 0;
}
}
}
This would produce:
Date and Time: 4/22/2002 3:08:37 PM
Minute Value: 08
Press any key to continue . . .
If the number is greater than 9, it would display the same.
|
ss |
Seconds |
A second value from 0 to 59.
If the number is less than 10, it would display with the leading 0 such as 04.
Here is an example:using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
string strSecond = time.ToString("ss");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Hour Value: {0}\n", strSecond);
return 0;
}
}
}
This would produce:Date and Time: 4/22/2002 3:08:07 PM
Hour Value: 07
Press any key to continue . . .
If the number is greater than 9, it would display like
that.
|
tt
or
tttt |
AM/PM |
Expression used to produce the
AM or PM side of a time value. Here is an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 5, 8, 7);
string strAMPM = time.ToString("tt");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("AM/PM Side: {0}\n", strAMPM);
return 0;
}
}
}
This would produce:
Date and Time: 4/22/2002 5:08:07 AM
AM/PM Side: AM
Press any key to continue . . .
|
To create or format a (complete) time value, you must use a
combination of characters:
Format |
Used For |
Description |
: |
Separator |
The character separator for time
values.
This character is set in the Regional (and Language) Settings of Control Panel.
Don't use this character by itself in a ToString() method. |
h or H |
Hours |
An hour number from 0 to 23.
If the hour is less than 10, it would display without the leading 0.
Don't use the letter "h" or "H" by itself in a ToString() method |
m |
Minutes |
A minute number from 0 to 59.
If the number is less than 10, it would display without the leading 0.
Here is an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
string strMinute = time.ToString("h:m");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Minute Value: {0}\n", strMinute);
return 0;
}
}
}
This would produce:
Date and Time: 4/22/2002 3:08:37 PM
Minute Value: 3:8
Press any key to continue . . .
If the minute number is greater than 9, it would display as such.
Don't use the letter "m" by itself in a ToString() method.
|
mm |
Minutes |
A minute number from 0 to 59.
If the number is less than 10, it would display with a leading 0.
Here is an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 37);
string strMinute = time.ToString("h:mm");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Minute Value: {0}\n", strMinute);
return 0;
}
}
}
This would produce:
Date and Time: 4/22/2002 3:08:37 PM
Minute Value: 3:08
Press any key to continue . . .
Don't pass "mm" by itself in a ToString() method
|
s |
Seconds |
A second value from 0 to 59.
If the number is less than 10, it would display without a leading 0.
Here is an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
string strSecond = time.ToString("h:mm:s");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Second Value: {0}\n", strSecond);
return 0;
}
}
}
This would produce:
Date and Time: 4/22/2002 3:08:07 PM
Minute Value: 3:08:7
Press any key to continue . . .
Don't pass "mm" by itself in a ToString() method
|
ss |
Seconds |
A second value from 0 to 59.
If the number is less than 10, it would display with a leading 0.
Here is an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
DateTime time = new DateTime(2002, 4, 22, 15, 8, 7);
string strSecond = time.ToString("h:mm:ss");
Console.WriteLine("Date and Time: {0}", time);
Console.WriteLine("Second Value: {0}\n", strSecond);
return 0;
}
}
}
This would produce:
Date and Time: 4/22/2002 3:08:07 PM
Minute Value: 3:08:07
Press any key to continue . . .
Don't pass "mm" by itself in a ToString() method
|
When combining these characters to create a format, you should abide by the rules of your
language, such as US English. You should refer to the formula set in the Time property page of the Regional (and Language) Settings of Control Panel.
Comparison Operations On Time Values |
|
As done for date values, you can compare time values to find out if one occurs
before another or whether they occur at the same time. To support the
comparisons, the DateTime structure is equipped with the Compare()
method.
The Current Date and Time |
|
When a user starts a computer and while using it, it keeps a date and time
values referred to as local date and time or system date and time. Depending on
your application, at one time you may need to get one or both of these pieces of
information. It is important to know that the computer, not you, controls this
information (but you can programmatically change it if you want). To support
both, the DateTime structure is equipped with a static property named Now.
This property holds the year, the month, the day, the name of the day, the hour,
the minute, and the second. Here is an example of calling it:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
Console.WriteLine("System Date and Time: {0}", DateTime.Now);
return 0;
}
}
}
Here is an example of running the program:
System Date and Time: 10/15/2006 3:08:48 PM
Press any key to continue . . .
To get the current date of the computer, the DateTime structure provides
a static property named Today. Here is an example:
using System;
namespace DateAndTime
{
class Program
{
static int Main()
{
Console.WriteLine("Current Date: {0}", DateTime.Today);
return 0;
}
}
}
Here is an example of running the program:
Current Date: 10/15/2006 12:00:00 AM
Press any key to continue . . .
Formatting Date and Time Combinations |
|
Although the DateTime structure is equipped to produce
default values of a date and time in combination, you can use the formats we
have reviewed to create your own date, in the sequences of your choice, such as
a time preceding a date, a value providing only the month and the minutes, etc.
|
|