Javascript Date.toString() 格式?

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

Javascript Date.toString() formatting?

javascriptdatetimestamptostring

提问by user1107685

Possible Duplicate:
Formatting a date in JavaScript

可能的重复:
在 JavaScript 中格式化日期

I have the following piece of script. It's a HTML5 slider with a date range. The slider is using a unix timestamp and I want to display the current selection in a readable format.

我有以下脚本。这是一个带有日期范围的 HTML5 滑块。滑块使用 unix 时间戳,我想以可读格式显示当前选择。

This is working fine but is outputting as "Wed May 16 2012 08:07:30 GMT+0100 (GMT Daylight Time)" despite me specifying the format as "yyyy-MM-dd HH:mm:ss".

尽管我将格式指定为“yyyy-MM-dd HH:mm:ss”,但这工作正常,但输出为“Wed May 16 2012 08:07:30 GMT+0100(GMT Daylight Time)”。

Any ideas why it's not outputting in my format?

任何想法为什么它不以我的格式输出?

<input id="slider3" type="range" min="1337149800" max="1337160600"
  step="450" onchange="printValue('slider3','rangeValue3')"/>
<input id="rangeValue3" type="text" size="90"/>

<script>
function printValue(sliderID, textbox) {
  var x = document.getElementById(textbox);
  var y = document.getElementById(sliderID);

  var d1=new Date(y.value*1000);

  var newtimestamp = d1.toString("yyyy-MM-dd HH:mm:ss");

  x.value = newtimestamp;
}
</script>

采纳答案by Daniel A. White

JavaScript's Dateobject does not support that. There's plenty of libraries to do this for you.

JavaScript 的Date对象不支持。有很多图书馆可以为你做这件事。