【问题标题】:Change datetime object into integer in html or jquery在 html 或 jquery 中将 datetime 对象更改为整数
【发布时间】:2019-07-18 03:29:20
【问题描述】:

我想将datetime.now() 对象转换为整数。

例如:

2019-07-18 11:16:33.325366 

应该转换成yyyymmddhhmmss格式:

20190718111633

这样可以使用sorttable.js 对我的日期列进行正确排序。

我应该如何在 html 中直接或使用 jquery 来做到这一点?任何帮助表示赞赏。

【问题讨论】:

  • 我认为即使是原始格式也应该正确排序?
  • 正如@Chaska 所说,“2019-07-18 11:16:33.325366”的格式已经可以排序了。
  • Sort date and time values 的可能重复项,虽然格式不相同,但可以按相同的方式排序。

标签: jquery html


【解决方案1】:

使用toISOString()replace()

let date = new Date().toISOString().substr(0, 19).replace(/-|T|:/g, '')

console.log(date)

【讨论】:

    【解决方案2】:

    打破你的日期并使用下面的 RegEx 替换。

    let d = new Date($.now());
    let result = d.getFullYear() + " " + (d.getMonth()+1) + " " + d.getDate() + " " + d.getHours() + " " + d.getMinutes() + " " + d.getSeconds();
    console.log(result.replace(/ /g, ""));
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    【讨论】:

      猜你喜欢
      • 2017-07-25
      • 2016-01-04
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多