【问题标题】:Convert timestamp to a specific date format in javascript [duplicate]在javascript中将时间戳转换为特定的日期格式[重复]
【发布时间】:2018-05-10 13:52:52
【问题描述】:

我正在尝试在 Javascript 中转换这种格式的时间戳

/日期(1231110000000)/

转成这种格式:

DD/MM/YYYY

有人知道怎么做吗??

【问题讨论】:

标签: javascript date timestamp date-format time-format


【解决方案1】:

如何将/Date(1231110000000)/ 转换为DD/MM/YYYY 格式:

function convert(timestamp) {
  var date = new Date(                          // Convert to date
    parseInt(                                   // Convert to integer
      timestamp.split("(")[1]                   // Take only the part right of the "("
    )
  );
  return [
    ("0" + date.getDate()).slice(-2),           // Get day and pad it with zeroes
    ("0" + (date.getMonth()+1)).slice(-2),      // Get month and pad it with zeroes
    date.getFullYear()                          // Get full year
  ].join('/');                                  // Glue the pieces together
}


console.log(convert("/Date(1231110000000)/"));

【讨论】:

    【解决方案2】:

    你的问题不清楚。

    如果您在第一个问题中的时间是 EPOCH 时间(从 1970 年过去的秒数),您可以使用 .toISOstring ,这应该可以让您足够接近您想要的。

    https://www.w3schools.com/jsref/jsref_toisostring.asp

    【讨论】:

    • 您尚未向 OP 展示如何将字符串解析为日期。 toISOString 返回 UTC 日期和时间,并且不是所需的格式。
    猜你喜欢
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 2016-11-26
    • 2014-11-17
    • 2015-10-26
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    相关资源
    最近更新 更多