如何在javascript中将字符串转换为Unix时间戳?

编程入门 行业动态 更新时间:2024-10-27 14:32:56
本文介绍了如何在javascript中将字符串转换为Unix时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将字符串2013-09-05 15:34:00转换为javascript中的Unix时间戳。谁能告诉你怎么做?谢谢。

I want to convert a string "2013-09-05 15:34:00" into a Unix timestamp in javascript. Can any one tell how to do that? thanks.

推荐答案

您可以初始化Date对象并调用getTime()以unix形式获取它。它以毫秒为单位,因此您需要除以1000才能在几秒钟内得到它。

You can initialise a Date object and call getTime() to get it in unix form. It comes out in milliseconds so you'll need to divide by 1000 to get it in seconds.

(new Date("2013/09/05 15:34:00").getTime()/1000)

它可能有小数位,所以将它包装在Math.round中就可以清除它。

It may have decimal bits so wrapping it in Math.round would clean that.

Math.round(new Date("2013/09/05 15:34:00").getTime()/1000)

更多推荐

如何在javascript中将字符串转换为Unix时间戳?

本文发布于:2023-10-23 01:36:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   中将   时间   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!