Javascript 将正常日期转换为 unix 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11893083/
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 07:34:55  来源:igfitidea点击:

Convert normal date to unix timestamp

javascriptjquerydatedatetimeunix-timestamp

提问by Stan

How can I convert normal date 2012.08.10to unix timestamp in javascript?

如何2012.08.10在javascript中将正常日期转换为unix时间戳?

Fiddle: http://jsfiddle.net/J2pWj/




I've seen many posts here that convert it in PHP, Ruby, etc... But I need to do this inside JS.

小提琴:http: //jsfiddle.net/J2pWj/




我在这里看到了很多用 PHP、Ruby 等转换它的帖子......但我需要在 JS 中执行此操作。

回答by fguillen

new Date('2012.08.10').getTime() / 1000

Check the JavaScript Date documentation.

检查JavaScript 日期文档

回答by thevinchi

parseInt((new Date('2012.08.10').getTime() / 1000).toFixed(0))

It's important to add the toFixed(0)to remove any decimals when dividing by 1000 to convert from milliseconds to seconds.

toFixed(0)在除以 1000 以从毫秒转换为秒时,添加以删除任何小数很重要。

The .getTime()function returns the timestamp in milliseconds, but true unix timestamps are always in seconds.

.getTime()函数以毫秒为单位返回时间戳,但真正的 unix 时间戳始终以秒为单位。

回答by Mahus

You should check out the moment.js api, it is very easy to use and has lots of built in features.

您应该查看 moment.js api,它非常易于使用并且具有许多内置功能。

I think for your problem, you could use something like this:

我认为对于您的问题,您可以使用以下方法:

var unixTimestamp = moment('2012.08.10', 'YYYY.MM.DD').unix();

回答by KARTHIKEYAN.A

var d = '2016-01-01T00:00:00.000Z';
console.log(new Date(d).valueOf()); // returns the number of milliseconds since the epoch

回答by hybrid9

You could simply use the unary + operator

您可以简单地使用一元 + 运算符

(+new Date('2012.08.10')/1000).toFixed(0);

(+new Date('2012.08.10')/1000).toFixed(0);

http://xkr.us/articles/javascript/unary-add/- look under Dates.

http://xkr.us/articles/javascript/unary-add/- 查看日期。

回答by Barmar

You can use Date.parse(), but the input formats that it accepts are implementation-dependent. However, if you can convert the date to ISO format (YYYY-MM-DD), most implementations should understand it.

您可以使用Date.parse(),但它接受的输入格式取决于实现。但是,如果您可以将日期转换为ISO 格式 (YYYY-MM-DD),则大多数实现都应该理解它。

See Why does Date.parse give incorrect results?.

请参阅为什么 Date.parse 给出不正确的结果?.

回答by dumitru

var date = new Date('2012.08.10');
var unixTimeStamp = Math.floor(date.getTime() / 1000);

In this case it's important to return only a whole number (so a simple division won't do), and also to only return actually elapsed seconds (that's why this code uses Math.floor()and not Math.round()).

在这种情况下,重要的是只返回一个整数(所以简单的除法不会做),并且只返回实际经过的秒数(这就是为什么这段代码使用Math.floor()而不是Math.round())。

回答by Nazar

You can do it using Date.parse() Method.

您可以使用 Date.parse() 方法来完成。

Date.parse($("#yourCustomDate).val())

Date.parse("03.03.2016") output-> 1456959600000

Date.parse("03.03.2016") 输出-> 1456959600000

Date.parse("2015-12-12") output-> 1449878400000

Date.parse("2015-12-12") 输出-> 1449878400000

回答by OptimusCrime

var datestr = '2012.08.10';
var timestamp = (new Date(datestr.split(".").join("-")).getTime())/1000;

回答by Damodar Bashyal

After comparing timestamp with the one from PHP, none of the above seems correct for my timezone. The code below gave me same result as PHP which is most important for the project I am doing.

将时间戳与 PHP 中的时间戳进行比较后,以上对于我的时区似乎都不正确。下面的代码给了我与 PHP 相同的结果,这对我正在做的项目最重要。

function getTimeStamp(input) {
    var parts = input.trim().split(' ');
    var date = parts[0].split('-');
 var time = (parts[1] ? parts[1] : '00:00:00').split(':');

 // NOTE:: Month: 0 = January - 11 = December.
 var d = new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);
 return d.getTime() / 1000;
}

// USAGE::
var start = getTimeStamp('2017-08-10');
var end = getTimeStamp('2017-08-10 23:59:59');

console.log(start + ' - ' + end);

I am using this on NodeJS, and we have timezone 'Australia/Sydney'. So, I had to add this on .env file:

我在 NodeJS 上使用它,我们有时区“澳大利亚/悉尼”。所以,我不得不在 .env 文件中添加这个:

TZ = 'Australia/Sydney'

Above is equivalent to:

以上相当于:

process.env.TZ = 'Australia/Sydney'