Programming

C# DateTime Now에서 날짜만 가져 오기

DragonTory 2020. 7. 17. 15:46
반응형

 

 

 

 

DateTime.Now 하면 현재 날짜와 시간을 얻어 올 수 있다. 

(년 월 일 시간 분 초 )

이 중에 시간은 0(자정)으로 초기화 시키고 날짜만 얻어 오고 싶으면 아래 예제 처럼

DateTime변수.Date

함수를 사용 하면 된다.

 

 

using System;

public class Example
{
   public static void Main()
   {
        DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
        Console.WriteLine(date1.ToString());

        // Get date-only portion of date, without its time.

        DateTime dateOnly = date1.Date;

        // Display date using short date string.
        Console.WriteLine(dateOnly.ToString("d"));
        // Display date using 24-hour clock.
        Console.WriteLine(dateOnly.ToString("g"));
        Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
   }
}
// The example displays output like the following output:
//       6/1/2008 7:47:00 AM
//       6/1/2008
//       6/1/2008 12:00 AM
//       06/01/2008 00:00

 

링크:

https://docs.microsoft.com/ko-kr/dotnet/api/system.datetime.date?view=netcore-3.1

 

 

반응형