New methods have been added to support converting DateTime to or from Unix time. The following APIs have been added to DateTimeOffset:
- static DateTimeOffset FromUnixTimeSeconds(long seconds)
- static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
- long ToUnixTimeSeconds()
- long ToUnixTimeMilliseconds()
So .NET 4.6 gives us some new methods, but to use them, you’ll first have to convert from DateTime to DateTimeOffset. First, make sure you’re targeting the right version of the .NET Framework:
You can then use the new methods:
1 2 3 | var dateTime = new DateTime(2015, 05, 24, 10, 2, 0, DateTimeKind.Local); var dateTimeOffset = new DateTimeOffset(dateTime); var unixDateTime = dateTimeOffset.ToUnixTimeSeconds(); |
…and to change back…
1 2 | var localDateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(unixDateTime) .DateTime.ToLocalTime(); |
Source: http://gigi.nullneuron.net/gigilabs/converting-tofrom-unix-timestamp-in-c/
'컴퓨터 Computer > C#' 카테고리의 다른 글
C# formatting (0) | 2009.11.29 |
---|---|
[기초,C#] 데이터형 (0) | 2009.10.04 |
[C#] 가계부 만들기1 (0) | 2008.10.02 |
Neters 2차 프로젝트 (0) | 2008.05.31 |
DB에 내용 입력하기 (InsertQuery) (0) | 2008.05.18 |