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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:23:51  来源:igfitidea点击:

Calculating milliseconds from epoch

javascriptdatetime

提问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 Dateobject

有些人发现有用的原始答案:另一种不需要实例化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

You can get the milliseconds from epochwith +new Date('date string').

你可以从毫秒时代+new Date('date string')

jsFiddle.

js小提琴

The +operator implicitly calls valueOf()which returns the same as getTime().

+运营商隐式调用valueOf()该方法返回相同的getTime()

回答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;