DateTime (날짜 / 시간)
docs.microsoft.com/en-us/dotnet/api/system.datetime?view=net-5.0
DateTime Struct (System)
Represents an instant in time, typically expressed as a date and time of day.
docs.microsoft.com
C#에서도 다른언어와 마찬가지로 날짜와 시간을 다룰수있는 클래스가 있다.
DateTime
자체에서 static
으로 불러와서 사용해도되고 new
키워드를 통해서 인스턴스화 해도 된다.
using System;
namespace datetime
{
class Program
{
static void Main(string[] args)
{
// Static
var staticNow = DateTime.Now;
// 인스턴스 생성
var dateTime = new DateTime(2021, 1, 1);
Console.WriteLine(dateTime);
// 오늘
var now = DateTime.Now;
Console.WriteLine($"Hour : {now.Hour}");
Console.WriteLine($"Minute : {now.Minute}");
// 내일
var tomrrow = now.AddDays(1);
// 어제
var yesterday = now.AddDays(-1);
// 여러가지 포맷으로도 가져올수있다.
Console.WriteLine(now.ToLongDateString());
Console.WriteLine(now.ToShortDateString());
Console.WriteLine(now.ToLongTimeString());
Console.WriteLine(now.ToShortTimeString());
// 날짜와 시간의 형태로 지정가능
Console.WriteLine(now.ToString("yyyy-MM-dd"));
Console.WriteLine(now.ToString("HH:mm"));
}
}
}
추가적으로 Datetime Format은 워낙 많아서 그때마다 찾아서 쓰는것이 편하다.
docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
Custom date and time format strings
Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.
docs.microsoft.com
'C# > Basic' 카테고리의 다른 글
override (오버라이드) (0) | 2021.03.13 |
---|---|
TimeSpan (시간) (0) | 2021.03.12 |
Path (0) | 2021.03.10 |
Directory, DirectoryInfo (0) | 2021.03.09 |
File, FileInfo (0) | 2021.03.08 |
댓글
이 글 공유하기
다른 글
-
override (오버라이드)
override (오버라이드)
2021.03.13 -
TimeSpan (시간)
TimeSpan (시간)
2021.03.12 -
Path
Path
2021.03.10 -
Directory, DirectoryInfo
Directory, DirectoryInfo
2021.03.09