[javascript] Javascript를 사용하여 특정 날짜의 epoch 가져 오기

07/26/2010Javascript를 사용하여 UNIX 타임 스탬프 로 어떻게 변환 합니까?



답변

Date개체를 만들고 호출 할 수 있습니다 getTime.

new Date(2010, 6, 26).getTime() / 1000


답변

new Date("2016-3-17").valueOf()

긴 시대를 반환합니다


답변

http://www.w3schools.com/jsref/jsref_obj_date.asp를 살펴보십시오 .

UTC()유닉스 시대에서 밀리 초를 반환 하는 함수 가 있습니다.


답변

Date.now () 함수를 사용할 수도 있습니다.


답변

일부 답변은 JavaScript Date 객체의 시간대 변형으로 인한 부작용을 설명하지 않습니다. 따라서 이것이 당신에게 관심이 있다면이 대답을 고려해야합니다.

방법 1 : 기계의 시간대에 따라 다름

기본적으로 JavaScript는 컴퓨터의 시간대를 고려하여 날짜를 반환하므로 getTime()결과는 컴퓨터마다 다릅니다. 실행중인이 동작을 확인할 수 있습니다.

new Date(1970, 0, 1, 0, 0, 0, 0).getTime()
    // Since 1970-01-01 is Epoch, you may expect ZERO
    // but in fact the result varies based on computer's timezone

시간대를 고려한 Epoch 이후 시간을 정말로 원한다면 이것은 문제가되지 않습니다. 따라서 현재 날짜 또는 컴퓨터의 시간대 에 따라 지정된 날짜에 대한 Epoch 이후 시간을 얻으려면 이 방법을 계속 사용할 수 있습니다.

// Seconds since Epoch (Unix timestamp format)

new Date().getTime() / 1000             // local Date/Time since Epoch in seconds
new Date(2020, 11, 1).getTime() / 1000  // time since Epoch to 2020-12-01 00:00 (local timezone) in seconds

// Milliseconds since Epoch (used by some systems, eg. JavaScript itself)

new Date().getTime()                    // local Date/Time since Epoch in milliseconds
new Date(2020,  0, 2).getTime()         // time since Epoch to 2020-01-02 00:00 (local timezone) in milliseconds

// **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)

방법 2 : 기계의 시간대 독립적

그러나 시간대의 변형을 가져오고 UTC로 지정된 날짜 (즉, 시간대 독립적) 대해 Epoch 이후 시간을 얻으려면 Date.UTC방법 을 사용 하거나 시간대에서 UTC로 날짜를 이동해야합니다.

Date.UTC(1970,  0, 1)
    // should be ZERO in any computer, since it is ZERO the difference from Epoch

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()  // difference in milliseconds between your timezone and UTC
    (new Date(1970,  0, 1).getTime() - timezone_diff)
    // should be ZERO in any computer, since it is ZERO the difference from Epoch

따라서이 방법을 사용하거나 차이를 빼면 결과는 다음과 같아야합니다.

// Seconds since Epoch (Unix timestamp format)

Date.UTC(2020,  0, 1) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()
    (new Date(2020,  0, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-01-01 00:00 UTC in seconds
    (new Date(2020, 11, 1).getTime() - timezone_diff) / 1000  // time since Epoch to 2020-12-01 00:00 UTC in seconds

// Milliseconds since Epoch (used by some systems, eg. JavaScript itself)

Date.UTC(2020,  0, 2)   // time since Epoch to 2020-01-02 00:00 UTC in milliseconds

    // Alternatively (if, for some reason, you do not want Date.UTC)
    const timezone_diff = new Date(1970, 0, 1).getTime()
    (new Date(2020,  0, 2).getTime() - timezone_diff)         // time since Epoch to 2020-01-02 00:00 UTC in milliseconds

// **Warning**: notice that MONTHS in JavaScript Dates starts in zero (0 = January, 11 = December)

IMO, 수행중인 작업을 알지 못하는 경우 (위의 참고 참조), 시스템 독립적이기 때문에 방법 2를 선호해야합니다 .


끝 노트

이 답변의 권장 사항은 Date.UTC지정된 날짜 / 시간 없이는 작동하지 않으므로 대체 접근 방식을 사용하고 다음과 같은 작업을 수행 할 수 있습니다.

const timezone_diff = new Date(1970, 0, 1).getTime()
(new Date().getTime() - timezone_diff)  // <-- !!! new Date() without arguments
    // means "local Date/Time subtracted by timezone since Epoch" (?)

이것은 의미가 없으며 아마도 잘못된 것입니다 (날짜를 수정하고 있음). 이것을하지 않도록주의하십시오. 현재 날짜로부터 신기원 이후 시간을 얻고 싶은 경우에 와 시간 , 당신은 아마 사용 OK입니다 방법 1 .


답변

Date.parse()메서드는 날짜의 문자열 표현을 구문 분석하고 이후 밀리 초 수를 반환합니다 January 1, 1970, 00:00:00 UTC.

const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');

console.log(unixTimeZero);
// expected output: 0

console.log(javaScriptRelease);
// expected output: 818035920000

자세히 알아보기 : Date.parse ()


답변

Number(new Date(2010, 6, 26))

위와 같은 방식으로 작동합니다. 몇 초가 필요한 경우 잊지 마세요/ 1000