달력

05

« 2012/05 »

  •  
  •  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  •  
  •  

'ASP.NET'에 해당되는 글 1

  1. 2010/11/19 Visual C# DateTime 관련 함수 정리
2010/11/19 17:16

Visual C# DateTime 관련 함수 정리 ASP.NET2010/11/19 17:16

// 프로그램 시작시간
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;
xmas = new DateTime(2008, 12, 25, 0, 0, 0);

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);


[출처] :
http://blog.visualp.com/148?category=1

'ASP.NET' 카테고리의 다른 글

Visual C# DateTime 관련 함수 정리  (0) 2010/11/19
Posted by eqzero

댓글을 달아 주세요