DateTime & TimeSpan

Posted by 나에요임마
2017. 9. 19. 10:58 Program/C#

우리나라 C#하는 사람들이 별로 없어서 그런걸까..
C#을 하는 사람이 없다기보다는.. C#으로 윈도우 프로그램을 만드는 사람이 별로 없는 것 같다.
델파이보다 오히려 더 적을지도...
아마추어들이 하려면 C++에 api mfc 핥아먹고 바로 C#으로 넘어오는 편이 더 나을 것 같은데...

--------

1.private void button1_Click(object sender, EventArgs e)
2.{
3.  TimeSpan asefnew 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.Timer fajklefjl = 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.Now;

// 1. 현재시간
DateTime now;
now = DateTime.Now;

Console.WriteLine(now); // 현재시간 전체
Console.WriteLine(now.Year); // 현재 년도
Console.WriteLine(now.Month); // 현재 달
Console.WriteLine(now.Day); // 현재 일
Console.WriteLine(now.DayOfWeek); // 현재 주
Console.WriteLine(now.DayOfYear); // 1년중 몇일째인지
Console.WriteLine(now.TimeOfDay); // 금일 자정부터 몇시간
Console.WriteLine(now.Hour); // 시간
Console.WriteLine(now.Minute); // 분
Console.WriteLine(now.Second); // 초
Console.WriteLine(now.Millisecond); // 1000분의 1초

Console.WriteLine(now.Ticks); // 1000분의 1초

// 2. 임의시간 설정
DateTime birthday;
birthday = DateTime.Parse("2009년8월9일"); // 시분초 미지정시 자정으로
birthday = DateTime.Parse("2009/08/09");

Console.WriteLine(birthday);

DateTime xmas;
xmasnew DateTime(20081225000);

Console.WriteLine(xmas);

// 3. 연산

// 3.1 오늘 + 100일
now = DateTime.Now;
DateTime result = now.AddDays(100); // 각 단위별 add메소드가 존재 MSDN참고

Console.WriteLine(result);

// 3.2 크리스마스까지 남은 날
TimeSpan result2 = xmas - now;
Console.WriteLine(result2);
Console.WriteLine(result2.Days); // NNN일 (내림표현)
Console.WriteLine(result2.TotalDays); // NNN.NNNNNNN일 (더정확)

// 3.3 오늘 - 100일
Console.WriteLine(now - TimeSpan.FromDays(100));
Console.WriteLine(now.AddDays(-100));

// 4. 날짜시간 출력형식 지정
Console.WriteLine(now.ToLocalTime()); // 2008-08-08 오전 10:31:25

Console.WriteLine(now.ToLongDateString()); // 2008년 8월 8일 금요일
Console.WriteLine(now.ToShortDateString()); // 2008-08-08

Console.WriteLine(now.ToLongTimeString()); // 오전 10:31:25
Console.WriteLine(now.ToShortTimeString());   // 오전 10:31

// 프로그램 종료시간
DateTime endTime = DateTime.Now;

Console.WriteLine("프로그램 수행시간 : {0}/ms", (double)(endTime - startTime).Ticks / 1000000.0F);

 

 

[ DateTimeFormatInfo ]

 

Format Character
Associated Property/ Description
dShortDatePattern
DLongDatePattern
fFull date and time (long date and short time)
FFullDateTimePattern (long date and long time)
gGeneral (short date and short time)
GGeneral (short date and long time)
m, MMonthDayPattern
r, RRFC1123Pattern
sSortableDateTimePattern (based on ISO 8601) using local time
tShortTimePattern
TLongTimePattern
uUniversalSortableDateTimePattern using the format for universal time display
UFull date and time (long date and long time) using universal time
y, YYearMonthPattern
  
dThe day of the month. Single-digit days will not have a leading zero.
ddThe day of the month. Single-digit days will have a leading zero.
dddThe abbreviated name of the day of the week, as defined in AbbreviatedDayNames.
ddddThe full name of the day of the week, as defined in DayNames.
MThe numeric month. Single-digit months will not have a leading zero.
MMThe numeric month. Single-digit months will have a leading zero.
MMMThe abbreviated name of the month, as defined in AbbreviatedMonthNames.
MMMMThe full name of the month, as defined in MonthNames.
yThe year without the century. If the year without the century is less than 10, the year is displayed with no leading zero.
yyThe year without the century. If the year without the century is less than 10, the year is displayed with a leading zero.
yyyyThe year in four digits, including the century.
ggThe period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string.
hThe hour in a 12-hour clock. Single-digit hours will not have a leading zero.
hhThe hour in a 12-hour clock. Single-digit hours will have a leading zero.
HThe hour in a 24-hour clock. Single-digit hours will not have a leading zero.
HHThe hour in a 24-hour clock. Single-digit hours will have a leading zero.
mThe minute. Single-digit minutes will not have a leading zero.
mmThe minute. Single-digit minutes will have a leading zero.
sThe second. Single-digit seconds will not have a leading zero.
ssThe second. Single-digit seconds will have a leading zero.
fThe fraction of a second in single-digit precision. The remaining digits are truncated.
ffThe fraction of a second in double-digit precision. The remaining digits are truncated.
fffThe fraction of a second in three-digit precision. The remaining digits are truncated.
ffffThe fraction of a second in four-digit precision. The remaining digits are truncated.
fffffThe fraction of a second in five-digit precision. The remaining digits are truncated.
ffffffThe fraction of a second in six-digit precision. The remaining digits are truncated.
fffffffThe fraction of a second in seven-digit precision. The remaining digits are truncated.
tThe first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any.
ttThe AM/PM designator defined in AMDesignator or PMDesignator, if any.
zThe 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".
zzThe time zone offset ("+" or "-" followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is "-08".
zzzThe 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 DateSeparator.
cWhere 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.
cWhere 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