Javascript kullanarak belirli bir tarih için epoch alın
Javascript kullanarak 07/26/2010 tarihini nasıl bir UNIX zaman damgasına dönüştürebilirim?
Cevap #1
Date nesnesi oluşturabilir ve üzerinde getTime metodunu çağırabilirsiniz:
new Date(2010, 6, 26).getTime() / 1000
Cevap #2
new Date("2016-3-17").valueOf()
uzun bir epoch döndürecek
Cevap #3
Bazı cevaplar JavaScript Date nesnesi için zaman dilimindeki değişikliklerin yan etkilerini açıklamıyor. Bu nedenle, bu sizin için bir endişe ise bu cevabı dikkate almalısınız.
Yöntem 1: Makinenin saat dilimine bağlı
Varsayılan olarak, JavaScript bir Tarihi makinenin zaman dilimini dikkate alarak döndürür, bu yüzden getTime() sonucu bilgisayardan bilgisayara değişir. Bu davranışı çalıştırarak kontrol edebilirsiniz:
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
Eğer saat diliminizi dikkate alarak Epoch'tan bu yana geçen zamanı gerçekten istiyorsanız, bu bir sorun değildir. Yani, geçerli tarih veya belirtilen bir tarih için bilgisayarın saat dilimine göre Epoch'tan bu yana geçen zamanı almak istiyorsanız, bu yöntemi kullanmaya devam edebilirsiniz.
// 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)
Yöntem 2: Makinenin saat diliminden bağımsız
Ancak, eğer saat dilimlerindeki farklılıklardan kurtulmak ve UTC'de belirtilmiş bir Tarih için Epoch'tan itibaren geçen zamanı almak istiyorsanız (yani, saat diliminden bağımsız olarak), Date.UTC yöntemini kullanmalı veya tarihi kendi saat diliminizden UTC'ye kaydırmalısınız:
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
Yani, bu yöntemi kullanarak (ya da alternatif olarak farkı çıkararak), sonuç şu şekilde olmalıdır:
// 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)
Bence, ne yaptığınızı bilmiyorsanız (yukarıdaki notu görün), makineden bağımsız olduğu için Yöntem 2'yi tercih etmelisiniz.
Sonek
Bu cevapta verilen önerilere rağmen, ve Date.UTC'in belirtilmiş bir tarih/saat olmadan çalışmaması nedeniyle, alternatif yöntemi kullanmaya ve şöyle bir şey yapmaya eğilimli olabilirsiniz:
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" (?)
Bu hiç mantıklı değil ve muhtemelen YANLIŞ (tarihi değiştiriyorsunuz). Bunu yapmamaya dikkat edin. Eğer geçerli tarihten VE SAAT itibariyle Epoch zamanını almak istiyorsanız, muhtemelen Yöntem 1'i kullanmanız uygundur.
