js 中日期 转换成时间戳 例如2013-08-30 转换为时间戳
js中传入指定日期转换为时间戳,可以使用原生javascript的Date对象进行格式化操作或第三方js库如moment.js进行格式化输出。以下是这2种方法的具体实现:
1、使用原生javascript的Date对象,进行格式化
(1)将日期字符串转换为Date类型的对象。
let d = new Date('2018-01-01')
(2)转换为时间戳。
let t = d.getTime(d) // 方法1
let t = d.valueOf(d) // 方法2
let t = d.parse(d) // 方法3
2、第三方js库moment.js进行格式化
(1)在html中引入moment.js,添加以下script标签:
<script src="https://cdn.bootcss.com/moment.js/2.20.1/moment.js"></script>
(2)将日期转换为时间戳
参数格式为:YYYY-MM-DD HH:mm:ss 或 YYYY-MM-DD
moment('2018-01-01').valueOf();
扩展资料:
1、js将当前日期转换为时间戳
let timeStamp = new Date().getTime()
2、js将时间戳转换为日期
let d = new Date(timestamp * 1000);// 时间戳为10位需*1000,时间戳为13位的话不需乘1000
let yyyy = d.getFullYear() + '-';
let MM = (d.getMonth()+1 < 10 ? '0'+(d.getMonth()+1) : d.getMonth()+1) + '-';
let dd = d.getDate() + ' ';
let HH = d.getHours() + ':';
let mm = d.getMinutes() + ':';
let ss = d.getSeconds();
return yyyy + MM + dd + HH + mm + ss;
参考资料来源:JavaScript标准库-Date
游促哈力: 以前遇到过一个关于时间戳的问题,为了不被大家鄙视,先说一下概念. 具体时间戳怎么定义的我也不清楚,但百度百科中有这么一句:“时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数”. 按这个定义,编程语言中倒是...
柳城县17744633502: js中怎么样时间格式转成时间戳 - ?
游促哈力: 将日期格式转换成时间戳: // 也很简单 date = new Date('2014-04-23 18:55:49:123'); //传入一个时间格式,如果不传入就是获取现在的时间了,就这么简单. // 有三种方式获取 time1 = date.getTime() time2 = date.valueOf() time3 = Date....
柳城县17744633502: js怎么把日期格式转换为日历显示格式,例如: - ?
游促哈力: 那只获取new Date().getHours()+':'+new Date().getMinutes() 就可以了
柳城县17744633502: js 怎么将日期字符串转换为时间戳? - ?
游促哈力: 前端有时候可能要从日期控件中拿到日期,然后参与计算,下边记录一个把日期字符串转换成时间戳的小函数. dateStr格式为“2014-05-08 00:22:11 ” function get_unix_time(dateStr) { var newstr = dateStr.replace(/-/g,'/'); var date = new Date(newstr); var time_str = date.getTime().toString(); return time_str.substr(0, 10); }
柳城县17744633502: js 字符串如何转换为时间格式??
游促哈力: var str = "2010-08-01";// 转换日期格式str = str.replace(/-/g, '/'); // "2010/08/01";// 创建日期对象var date = new Date(str);// 加一天date.setDate(date.getDate() + 1);// 没有格式化的功能,只能一个一个取str = date.getFullYear() + '-' // 因...
柳城县17744633502: JS 中2015年04月07日日期格式怎么转换成时间戳格式 - ?
游促哈力: function get_unix_time(dateStr) {dateStr = dateStr.replace('年','-');dateStr = dateStr.replace('月','-');dateStr = dateStr.replace('日','-');var newstr = dateStr.replace(/-/g,'/');var date = new Date(newstr);var time_str = date....
柳城县17744633502: js时间戳怎么转成日期格式 - ?
游促哈力: javascript 时间戳自定义格式转换,支持年月日时分秒等多种形式组合的日期和时间. 示例 年、月、日、时、分、秒 var date = jutils.formatDate(new Date(1533686888*1000),"YYYY-MM-DD HH:ii:ss"); console.log(date); // 2019-07-09 19:44:01更多自定义参数和用法可以参照:javascript格式化日期时间 下面是源码的截图: js时间戳转为日期格式
柳城县17744633502: 求JS时间字符串转换成时间戳 - ?
游促哈力:代码如下所示:先转成date就行了// 获取某个时间格式的时间戳var stringTime = "1990-01-01 ";var timestamp = Date.parse(new Date(stringTime));timestamp = timestamp / 1000;//1990-01-01 时间戳console.log(stringTime + "的时间戳为:" + timestamp);
柳城县17744633502: js中怎么把只有年月的日期转为时间戳 - ?
游促哈力: 支持一下感觉挺不错的
柳城县17744633502: 如何用js获取特定时间戳 - ?
游促哈力: var formatTime = function(time = new Date(), format) { const TOTOW = e => `0${e}`.substr(-2); // 转成2位的格式 1 => 01 const date = new Date(time); const yyyy = date.getFullYear(); const MM = TOTOW(date.getMonth() + 1); const dd = TOTOW(...