Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:如何获取2020-05-15T05:00:00Z格式的日期字符串?_Javascript_Date_Datetime - Fatal编程技术网

Javascript:如何获取2020-05-15T05:00:00Z格式的日期字符串?

Javascript:如何获取2020-05-15T05:00:00Z格式的日期字符串?,javascript,date,datetime,Javascript,Date,Datetime,我有一个javascript中的date对象,我试图让它以该格式返回一个字符串 private convertEpochToDate(epoch: string) { var d = new Date(0); // The 0 there is the key, which sets the date to the epoch if (!epoch) return "nodate"; var date = new Date(0); let mi

我有一个javascript中的date对象,我试图让它以该格式返回一个字符串

private convertEpochToDate(epoch: string) {
    var d = new Date(0); // The 0 there is the key, which sets the date to the epoch
    if (!epoch) return "nodate";
    var date = new Date(0);
    let milliseconds = parseInt(epoch);
    date.setUTCMilliseconds(milliseconds);

    return date;
}
我需要将日期格式化为如下字符串,而不是date对象:2020-05-15T05:00:00Z

console.log(newdate().toISOString())您应该查看具有您想要的各种日期功能的库。

convertedepochtodate(epochTime:number)表示它是必需的。所以你不需要

if (!epoch) return "nodate";
convertEpochToDate(epochTime?:number),现在它是可选的

if (!epoch) return "nodate";
或者

(epochTime:number)因为您期望的日期是以毫秒为单位的数字-例如:new date().getTime()

(epochTime:Date)如果作为日期对象传递。记住这是“日期”,不是“日期”


使用
newdate().toISOString()
我说过要使用jQuery吗?不js不需要jQuery,关键是“use”不是一个有用的答案。这可能是一个合理的评论。我不明白为什么它没有用处,因为它可以帮助解决这个问题,并且很可能解决任何与未来日期相关的问题。
const epochString = !!date ? this.convertEpochToDate(date) : "no date";
private convertEpochToDate(epochTime: number): string => new Date(epochTime).toISOString();
 private convertEpochToDate(epochTime: Date): string => epochTime.toISOString();