【问题标题】:javascript datetime in custom format as per timezone [duplicate]根据时区自定义格式的javascript日期时间[重复]
【发布时间】:2019-06-17 19:37:39
【问题描述】:

我想要如下自定义格式的日期时间。

2019-01-24T05:52:29:420 +0000 GMT+0000

但是当我尝试使用以下语法时,我没有工作。

var currentdate = new Date(); 
alert(currentdate);

有什么帮助吗?谢谢

【问题讨论】:

标签: javascript date


【解决方案1】:

看看 Moment.js,你可以很容易地用它来格式化 JS 日期。

例如:

moment().format('MMMM Do YYYY, h:mm:ss a'); // January 24th 2019, 5:13:29 pm
moment().format('dddd');                    // Thursday

在你的情况下:

moment(currentdate).format('the format you would like');

https://momentjs.com/

【讨论】:

  • 我想用纯 javascript 输出。
  • 您不想使用任何库?为什么?
  • 我不想使用任何库。我正在使用 Angular 应用程序
【解决方案2】:

它非常简单,您只需执行 currentdate.toISOString() 将返回“2019-01-24T06:19:19.975Z”,现在它只需从字符串中删除最后一个字符并连接其他字符串部分在里面。

var currentdate = new Date().toISOString();

alert(currentdate.substr(0, currentdate.length-1)+' +0000 GMT+0000');

【讨论】:

  • 以上是正确答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
相关资源
最近更新 更多