【问题标题】:How to format date in Javascript in this format: 2021-12-29T15:04:09.0129998 [duplicate]如何以这种格式在 Javascript 中格式化日期:2021-12-29T15:04:09.0129998 [重复]
【发布时间】:2021-12-29 18:23:45
【问题描述】:

我有一个 .NET 应用程序,它以这种格式返回日期:2021-12-29T15:04:09.0129998。 我需要一个 Javascript 表单来格式化为“2021 年 12 月 29 日”。

到目前为止,我发现的所有示例都使用“新日期”来举例说明,例如这里的示例:

const currentDate = new Date();
console.log(currentDate.toLocaleDateString('de-DE'));

创建一个与我的格式不同的日期,然后它就可以工作了。就我而言,它不起作用。

【问题讨论】:

  • 这里有一个提示:const currentDate = new Date('2021-12-29T15:04:09.0129998');
  • new Date().toLocaleDateString('en',{month: 'long', day: 'numeric', year: 'numeric'}) 会非常接近。注意new Date('2021-12-29T15:04:09.0129998'),它会被解析为本地或无效日期,见Why does Date.parse give incorrect results?

标签: javascript .net date format


【解决方案1】:
new Date().toDateString("de-DE").substring(3)

【讨论】:

  • 这对我不起作用,我有这种格式的日期:“2021-12-29T15:04:09.0129998”不同于“新日期”;
  • 如果你想转换成这种格式,请使用这个新的 Date("2021-12-29T15:04:09.0129998").toDateString().substring(3)
  • 返回类似“30.12.2021”的日期(可能)。
  • 有效:“new Date("2021-12-29T15:04:09.0129998").toDateString().substring(3)" 感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多