Javascript 从纪元计算毫秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9011758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating milliseconds from epoch
提问by deruse
Given that I have: mm, dd, yy, hh:mm, am/pm, what is the recommended/easiest way to convert that data into milliseconds from epoch?
鉴于我有:mm、dd、yy、hh:mm、am/pm,将数据转换为纪元毫秒的推荐/最简单方法是什么?
回答by Chris
Everytime I get an upvote on this answer, I'm reminded that my answer doesn't actually answerthe question.
每次我对这个答案点赞时,都会提醒我我的答案实际上并没有回答这个问题。
OP: If you can parse your string into the following format, you can use one of the other answers. MM/DD/YYYY HH:MM:SS AM EDT
(It's important to specify the timezone; epoch is based on the meridian)
OP:如果您可以将字符串解析为以下格式,则可以使用其他答案之一。MM/DD/YYYY HH:MM:SS AM EDT
(指定时区很重要;纪元基于子午线)
let milliseconds = new Date("1/20/1982 5:00 PM EST").getTime()
console.log(milliseconds);
Original Answer that some people have found useful:Another technique (which most browsers support, as well as NodeJS) that doesn't require having to instantiate a Date
object
有些人发现有用的原始答案:另一种不需要实例化Date
对象的技术(大多数浏览器都支持,以及 NodeJS)
var nowEpoch = Date.now();
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
回答by SLaks
new Date("some string").getTime()
回答by alex
回答by Joseph Dailey
var date;
if(pm)
date = new Date(yy, mm, dd, hh+12, mm, 0, 0);
else
date = new Date(yy, mm, dd, hh, mm, 0, 0);
var millis = date.getTime();
var seconds = millis/1000;
回答by Murthii Ch
var myDate = new Date(Timestamp); // Your timezone! either timestamp ot date var myEpoch = myDate.getTime()/1000.0;
var myDate = 新日期(时间戳);// 你的时区!时间戳或日期 var myEpoch = myDate.getTime()/1000.0;