Javascript - 将时间转换为整数和整数到时间

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

Javascript - Convert Time to Integer and Integer to Time

javascriptnode.jstime

提问by Srikanth Jeeva

This is what I do in Ruby.

这就是我在 Ruby 中所做的。

time = Time.now
=> 2013-10-08 12:32:50 +0530
time.to_i //converts time to integer
=> 1381215770
Time.at(time.to_i) //converts integer to time
=> 2013-10-08 12:32:50 +0530

I'm trying to implement the same with Node.js, but not sure how to do it. Kindly help me in finding a module for implementing the same with Node.js, Javascript. Thanks!

我正在尝试使用 Node.js 实现相同的功能,但不知道该怎么做。请帮助我找到一个使用 Node.js、Javascript 实现相同功能的模块。谢谢!

回答by user10

In javascript world.

在javascript世界中。

Date.now()

and

new Date(1381216317325);

回答by Dmitry Matveev

In addition to user10 answer

除了 user10 回答

Date.parse("2013-10-08 12:32:50 +0530");

will get you time as integer

会让你的时间为整数

EDIT
Date API

编辑
日期 API

回答by Endre Simo

new Date().getTime(); 

will return an integer which represent the time in milliseconds spent since midnight 01 January, 1970 UTC. This need to be parsed somehow to be more human readable.

将返回一个整数,表示自 UTC 1970 年 1 月 1 日午夜以来所花费的时间(以毫秒为单位)。这需要以某种方式进行解析,以使其更具人类可读性。

There is no default method implemented in Javascript which translate this number to a human interpretable date, so you have to write yourself.

Javascript 中没有实现将这个数字转换为人类可解释日期的默认方法,因此您必须自己编写。

A simple method would be this:

一个简单的方法是这样的:

function getTime() {
    var now = new Date();
    return ((now.getMonth() + 1) + '-' +
            (now.getDate()) + '-' +
             now.getFullYear() + " " +
             now.getHours() + '-' +
             ((now.getMinutes() < 10)
                 ? ("0" + now.getMinutes())
                 : (now.getMinutes())) + ':' +
             ((now.getSeconds() < 10)
                 ? ("0" + now.getSeconds())
                 : (now.getSeconds())));
}

console.log(getTime());

You can adjust yourself the order of appearance.

您可以自行调整出现顺序。