목표
현재 시간 표시하기
- DataTime.Now를 출력하면 일/월/년도 시간:분:초 {오전/오후}가 표시된다.
using System;
public class TimeUtils
{
public string GetCurrentDate()
{
return DateTime.Now.ToString();
}
}
현재 시간 포맷 지정
- format을 지정하면 년/월/일/시/분/초/오전,오후 값을 원하는 형태로 출력할 수 있다.
- yyyy : 년도
- MM : 월
- dd : 일
- HH : 시
- mm : 분
- ss : 초
- tt : 오전, 오후
using System;
public class TimeUtils
{
public static string GetCurrentDate()
{
return DateTime.Now.ToString(("yyyy-MM-dd HH:mm:ss tt"));
}
public static string GetYear()
{
return DateTime.Now.ToString(("yyyy"));
}
}
Unix Time으로 변환하기
using System;
public class TimeUtils
{
public static double GetCurrentMilliSeconds()
{
TimeSpan time = (DateTime.UtcNow - new DateTime(1970,1,1));
return t.TotalMilliseconds;
}
public static string GetMilliSecondsToDate(double milliSecond)
{
var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddMilliseconds((double)expiredTime).ToLocalTime();
return dateTime.ToString();
}
}