[C#] C #에서 올바른 타임 스탬프를 얻는 방법

내 응용 프로그램에서 유효한 타임 스탬프를 얻고 싶습니다.

public static String GetTimestamp(DateTime value)
{
    return value.ToString("yyyyMMddHHmmssffff");
}
//  ...later on in the code
String timeStamp = GetTimestamp(new DateTime());
Console.WriteLine(timeStamp);

산출:

000101010000000000

나는 다음과 같은 것을 원했다.

20140112180244

내가 뭘 잘못 했니?



답변

실수 new DateTime()로 현재 날짜 및 시간 대신 00 : 00 : 00.000에 0001을 1 월 1 일로 반환하는을 사용 하고 있습니다. 현재 날짜와 시간을 얻는 올바른 구문은 DateTime.Now 이므로 다음을 변경하십시오.

String timeStamp = GetTimestamp(new DateTime());

이에:

String timeStamp = GetTimestamp(DateTime.Now);


답변

var Timestamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();


답변

var timestamp = DateTime.Now.ToFileTime();

//output: 132260149842749745

이것은 별개의 거래를 개별화하는 대체 방법입니다. 유닉스 시간이 아니라 Windows 파일 시간입니다.

로부터 문서 :

A Windows file time is a 64-bit value that represents the number of 100-
nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601
A.D. (C.E.) Coordinated Universal Time (UTC).


답변

Int32 unixTimestamp = (Int32)(TIME.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

“TIME”은 유닉스 타임 스탬프를 얻고 자하는 DateTime 객체입니다.


답변

대한 UTC :

string unixTimestamp = Convert.ToString((int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);

로컬 시스템의 경우 :

string unixTimestamp = Convert.ToString((int)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);


답변