【问题标题】:How can I convert my date "Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time) " to JAN 01,2020 using JavaScript?如何使用 JavaScript 将我的日期“2020 年 1 月 1 日星期三 00:00:00 GMT+0530(印度标准时间)”转换为 2020 年 1 月 1 日?
【发布时间】:2021-01-01 13:54:25
【问题描述】:

目前我正在从数据库中获取格式为 Wed Jan 01 2020 00:00:00 GMT+0530(印度标准时间)的日期,但我想要 2020 年 1 月 1 日格式的日期,但我不想要为此使用 mon=ment.js。还有其他方法可以实现吗?

【问题讨论】:

  • 你可以试试new Date().toLocaleDateString()。这里有很多例子w3schools.com/jsref/…
  • 我已经尝试过这种方法不起作用。
  • var d = document.getElementsByClassName("date")[0];控制台.log(d); var str = $.datepicker.formatDate('M dd, yy', d); $('#findate').innerHTML(str);
  • sri laskshmi 我使用toLocaleDateString()添加了我的答案

标签: javascript jquery mongodb express


【解决方案1】:

这是一个没有moment.js的解决方案

var n = new Date("Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time)");
var options = {
  month: "short",
  day: "numeric",    
  year: "numeric",
  timeZone: 'IST'
}
document.write(n.toLocaleString("en-EN", options));

【讨论】:

    【解决方案2】:

    你可以简单地使用 moment.js

    import * as moment from 'moment';
    
    moment('your_date_variable').format('DD/MM/YYYY');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-14
      • 2011-01-30
      • 1970-01-01
      • 2012-02-19
      • 1970-01-01
      • 2023-01-13
      • 2016-12-27
      相关资源
      最近更新 更多