node.js 在javascript中将时间戳转换为人工日期的函数

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

Function to convert timestamp to human date in javascript

javascriptnode.jsdateexpresstimestamp

提问by Willy Mularto

How to convert this timestamp 1382086394000to 2013-10-18 08:53:14using a function in javascript? Currently I have this function:

如何将此时间戳转换13820863940002013-10-18 08:53:14使用 javascript 中的函数?目前我有这个功能:

function cleanDate(d) {return new Date(+d.replace(/\/Date\((\d+)\)\//, ''));}

回答by RobG

The value 1382086394000is probably a time value, which is the number of milliseconds since 1970-01-01T00:00:00Z. You can use it to create an ECMAScript Date object using the Date constructor:

1382086394000可能是一个时间值,它是自 1970-01-01T00:00:00Z 以来的毫秒数。您可以使用它通过Date 构造函数创建 ECMAScript Date 对象:

var d = new Date(1382086394000);

How you convert that into something readable is up to you. Simply sending it to output should call the internal (and entirely implementation dependent)toStringmethod* that usually prints the equivalent systemtime in a human readable form, e.g.

您如何将其转换为可读的内容取决于您。简单地将它发送到输出应该调用内部(并且完全依赖于实现)toString方法*,该方法通常以人类可读的形式打印等效的系统时间,例如

Fri Oct 18 2013 18:53:14 GMT+1000 (EST) 

In ES5 there are some other built-in formatting options:

在 ES5 中还有一些其他的内置格式选项:

and so on. Note that most are implementation dependent and will be different in different browsers. If you want the same format across all browsers, you'll need to format the date yourself, e.g.:

等等。请注意,大多数都依赖于实现,并且在不同的浏览器中会有所不同。如果您希望在所有浏览器中使用相同的格式,则需要自己格式化日期,例如:

alert(d.getDate() + '/' + (d.getMonth()+1) + '/' + d.getFullYear());

* The format of Date.prototype.toStringhas been standardised in ECMAScript 2018. It might be a while before it's ubiquitous across all implementations, but at least the more common browsers support it now.

* Date.prototype.toString的格式已在ECMAScript 2018标准化。它可能需要一段时间才能在所有实现中无处不在,但至少现在更常见的浏览器支持它。

回答by Optimus Krish

This works fine. Checked in chromebrowser:

这工作正常。在chrome浏览器中检查:

var theDate = new Date(timeStamp_value * 1000);
dateString = theDate.toGMTString();
alert(dateString );

回答by Scary Wombat

why not simply

为什么不简单

new Date (timestamp);

A date is a date, the formatting of it is a different matter.

日期是一个日期,它的格式是另一回事。

回答by svarog

Moment.jscan convert unix timestamps into any custom format

Moment.js可以将 Unix 时间戳转换为任何自定义格式

In this case : var time = moment(1382086394000).format("DD-MM-YYYY h:mm:ss");

在这种情况下 : var time = moment(1382086394000).format("DD-MM-YYYY h:mm:ss");

will print 18-10-2013 11:53:14;

将打印18-10-2013 11:53:14

Here's a plunkerthat demonstrates this.

这是一个证明这一点的plunker

回答by Trojan

Here are the simple ways to every date format confusions:

以下是解决各种日期格式混淆的简单方法:

for current date:

当前日期:

var current_date=new Date();

to get the Timestamp of current date:

获取当前日期的时间戳:

var timestamp=new Date().getTime();

to convert a particular Date into Timestamp:

将特定日期转换为时间戳:

var timestamp_formation=new Date('mm/dd/yyyy').getTime();

to convert timestamp into Date:

将时间戳转换为日期:

    var timestamp=new Date('02/10/2016').getTime();
    var todate=new Date(timestamp).getDate();
    var tomonth=new Date(timestamp).getMonth()+1;
    var toyear=new Date(timestamp).getFullYear();
    var original_date=tomonth+'/'+todate+'/'+toyear;

  OUTPUT:
 02/10/2016

回答by KARTHIKEYAN.A

we need to create new function using JavaScript.

我们需要使用 JavaScript 创建新函数。

function unixTime(unixtime) {

    var u = new Date(unixtime*1000);

      return u.getUTCFullYear() +
        '-' + ('0' + u.getUTCMonth()).slice(-2) +
        '-' + ('0' + u.getUTCDate()).slice(-2) + 
        ' ' + ('0' + u.getUTCHours()).slice(-2) +
        ':' + ('0' + u.getUTCMinutes()).slice(-2) +
        ':' + ('0' + u.getUTCSeconds()).slice(-2) +
        '.' + (u.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) 
    };

console.log(unixTime(1370001284))

2016-04-30 08:36:26.000

回答by Black Mamba

formatDateis the function you can call it and pass the date you want to format to dd/mm/yyyy

formatDate是您可以调用它并将要格式化的日期传递给的函数 dd/mm/yyyy

var unformatedDate = new Date("2017-08-10 18:30:00");
 
$("#hello").append(formatDate(unformatedDate));
function formatDate(nowDate) {
 return nowDate.getDate() +"/"+ (nowDate.getMonth() + 1) + '/'+ nowDate.getFullYear();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="hello">


</div>

回答by Roman

My ES6 variant produces a string like this 2020-04-05_16:39:45.85725. Feel free to modify the return statement to get the format that you need:

我的 ES6 变体产生这样的字符串2020-04-05_16:39:45.85725。随意修改 return 语句以获得您需要的格式:

const getDateStringServ = timestamp => {

  const plus0 = num => `0${num.toString()}`.slice(-2)

  const d = new Date(timestamp)

  const year = d.getFullYear()
  const monthTmp = d.getMonth() + 1
  const month = plus0(monthTmp)
  const date = plus0(d.getDate())
  const hour = plus0(d.getHours())
  const minute = plus0(d.getMinutes())
  const second = plus0(d.getSeconds())
  const rest = timestamp.toString().slice(-5)

  return `${year}-${month}-${date}_${hour}:${minute}:${second}.${rest}`
}

回答by Athar

To calculate date in timestamp from the given date

从给定日期计算时间戳中的日期

//To get the timestamp datefrom normal date: In format - 1560105000000

//从正常日期获取时间戳日期:格式 - 1560105000000

//input datecan be in format : "2019-06-09T18:30:00.000Z"

//输入日期可以是格式:“2019-06-09T18:30:00.000Z”

this.calculateDateInTimestamp = function (inputDate) {
    var date = new Date(inputDate);
    return date.getTime();
}

output: 1560018600000

输出:1560018600000