DateTime & TimeSpan
우리나라 C#하는 사람들이 별로 없어서 그런걸까..
C#을 하는 사람이 없다기보다는.. C#으로 윈도우 프로그램을 만드는 사람이 별로 없는 것 같다.
델파이보다 오히려 더 적을지도...
아마추어들이 하려면 C++에 api mfc 핥아먹고 바로 C#으로 넘어오는 편이 더 나을 것 같은데...
--------
1.
private
void
button1_Click(
object
sender, EventArgs e)
2.
{
3.
TimeSpan asef =
new
TimeSpan( 15,30,00);
4.
Console. WriteLine( asef );
5.
Console.WriteLine(DateTime.Now.Add(asef));
//현재시간에 15시간을 더하기
6.
Console.WriteLine(DateTime.Now.TimeOfDay);
//0시부터 현재시간까지를 TimeSpan값으로 반환
7.
Console.WriteLine(TimeSpan.Parse(
"15:30:00"
));
//TimeSpan값을 지정.. 문자열
8.
}
DateTime과 TimeSpan 둘 다 시간을 표현한다.
다른점은...
DateTime은 몇월몇일몇시몇분을 달력이나 시계를 표현하고
TimeSpan은 1시간, 2시간,,,... 이런걸 표현한다고 할까..
시간과 시각?
전자시계 개념으로 설면하자면... 10분짜리 타이머를 맞춘다면...
TimeSpan .. 알람을 맞추면..DateTime이렇게 보면 쉬울까...
DateTime - DateTime은 TimeSpan자료형이 된다.
(DateTime)2011년 2월 16일 21시 - (DateTime)2011년 2월 16일 15시 = (TimeSpan)6시간
위의 소스코드만으로도 설명은 충분할 것 같지만.... 추가하자면...
8시부터 뭔가를 실행시키고 싶다! 라고 한다면....
타이머 돌리고... System. Windows. Forms. Timerfajklefjl = new [tab] ();
if ( DateTime. Now. TimeOfDay >TimeSpan. Parse( "08:00:00"))
//***********************************************************************************************************
[펌] http://cafe.daum.net/vs.net/4sG8/20?docid=1HLyr|4sG8|20|20090326223655&q=c%23%20datetime
프로그램 시작시간
DateTime startTime = DateTime
// 1. 현재시간
Console.WriteLine(now); // 현재시간 전체
Console.WriteLine(now.Year); // 현재 년도
Console
Console
Console
Console.WriteLine(now.DayOfYear); // 1년중 몇일째인지
Console.WriteLine(now.TimeOfDay); // 금일 자정부터 몇시간
Console
Console
Console
Console
Console
// 2. 임의시간 설정
birthday = DateTime.Parse("2009년8월9일"); // 시분초 미지정시 자정으로
Console
Console
// 3. 연산
// 3.1 오늘 + 100일
DateTime result = now.AddDays(100); // 각 단위별 add메소드가 존재 MSDN참고
Console
// 3.2 크리스마스까지 남은 날
Console
Console.WriteLine(result2.Days); // NNN일 (내림표현)
Console.WriteLine(result2.TotalDays); // NNN.NNNNNNN일 (더정확)
// 3.3 오늘 - 100일
Console
Console
// 4. 날짜시간 출력형식 지정
Console
Console.WriteLine(now.ToLongDateString()); // 2008년 8월 8일 금요일
Console
Console
Console
// 프로그램 종료시간
Console.WriteLine("프로그램 수행시간 : {0}/ms", (double)(endTime - startTime).Ticks / 1000000.0F);
Format Character | Associated Property/ Description |
d | |
D | |
f | Full date and time (long date and short time) |
F | |
g | General (short date and short time) |
G | General (short date and long time) |
RFC1123Pattern | |
s | |
t | |
T | |
U | Full date and time (long date and long time) using universal time |
d | The day of the month. Single-digit days will not have a leading zero. |
The day of the month. Single-digit days will have a leading zero. | |
The abbreviated name of the day of the week, as defined in AbbreviatedDayNames. | |
The full name of the day of the week, as defined in DayNames. | |
M | The numeric month. Single-digit months will not have a leading zero. |
MM | The numeric month. Single-digit months will have a leading zero. |
MMM | The abbreviated name of the month, as defined in AbbreviatedMonthNames. |
MMMM | The full name of the month, as defined in MonthNames. |
The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. | |
The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero. | |
The year in four digits, including the century. | |
The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string. | |
h | The hour in a 12-hour clock. Single-digit hours will not have a leading zero. |
The hour in a 12-hour clock. Single-digit hours will have a leading zero. | |
H | The hour in a 24-hour clock. Single-digit hours will not have a leading zero. |
HH | The hour in a 24-hour clock. Single-digit hours will have a leading zero. |
m | The minute. Single-digit minutes will not have a leading zero. |
The minute. Single-digit minutes will have a leading zero. | |
s | The second. Single-digit seconds will not have a leading zero. |
The second. Single-digit seconds will have a leading zero. | |
f | The fraction of a second in single-digit precision. The remaining digits are truncated. |
The fraction of a second in double-digit precision. The remaining digits are truncated. | |
The fraction of a second in three-digit precision. The remaining digits are truncated. | |
The fraction of a second in four-digit precision. The remaining digits are truncated. | |
The fraction of a second in five-digit precision. The remaining digits are truncated. | |
The fraction of a second in six-digit precision. The remaining digits are truncated. | |
The fraction of a second in seven-digit precision. The remaining digits are truncated. | |
t | The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. |
The AM/PM designator defined in AMDesignator or PMDesignator, if any. | |
z | The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is "-8". |
The time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08". | |
The full time zone offset ("+" or "-" followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is "-08:00". | |
: | The default time separator defined in TimeSeparator. |
/ | The default date separator defined in |
% c | Where c is a format pattern if used alone. The "%" character can be omitted if the format pattern is combined with literal characters or other format patterns. |
\ c | Where c is any character. Displays the character literally. To display the backslash character, use "\\". |
Only format patterns listed in the second table above can be used to create custom patterns; standard format characters listed in the first table cannot be used to create custom patterns. Custom patterns are at least two characters long; for example,
DateTime . Now. ToString( "d") returns the DateTime value; "d" is the standard short date pattern.- DateTime.Now.ToString( "%d") returns the day of the month; "%d" is a custom pattern.
- DateTime.Now.ToString( "d ") returns the day of the month followed by a white-space character; "d " is a custom pattern.
'Program > C#' 카테고리의 다른 글
시리얼통신 (1) | 2017.10.02 |
---|---|
추상화클래스와 인터페이스의 용도, 차이점, 공통점 (0) | 2017.09.19 |
쓰레드 - Thread 클래스 (0) | 2017.09.19 |
STAThread (0) | 2017.08.23 |
프로세스 스케줄링(Process Scheduling) (0) | 2017.07.05 |