js时间戳转换日期,日期转换时间戳
生活随笔
收集整理的這篇文章主要介紹了
js时间戳转换日期,日期转换时间戳
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
日期轉換時間戳
支持獲取當前提供日期之前或之后的n天、n小時、n分鐘、n秒鐘
/*** 獲取時間戳* @param date 日期:2022/02/19 13:49:55* @param num 當前日期的前或后推幾天/小時/分鐘/秒* @param type d天/h小時/m分鐘/s秒* @returns {number}*/timestamps: (date = '', num = 0, type = 'd') => {let today;if (!date) {today = (new Date()).getTime();} else {today = (new Date(date)).getTime();}let result;if (!!num) {num = parseInt(num);if (type === 'h') {result = today + 3600 * num * 1000;} else if (type === 'm') {result = today + 60 * num * 1000;} else if (type === 's') {result = today + num * 1000;} else {result = today + 3600 * 24 * num * 1000;}} else {result = today;}return result;}時間戳轉換日期
支持自定義連接符,支持是否保留時分秒等等
/*** 根據時間戳獲取日期* @param stamps 毫秒級時間戳* @param date 返回日期* @param time 返回時間* @param link 日期鏈接符* @param has_minutes 是否保留分* @param has_second 是否保留秒* @param time_link 時間鏈接符* @returns {string}*/ datestamps: (stamps = 0, date = true, time = true, link = "/", has_minutes = true, has_second = true, time_link = ":") => {if (!stamps) {stamps = Date.now();}let now = new Date(stamps);let result = '';if (date) {result += now.getFullYear() + link+ (((now.getMonth() + 1).toString().length === 1) ? ('0' + (now.getMonth() + 1)) : (now.getMonth() + 1)) + link+ ((now.getDate().toString().length === 1) ? ('0' + now.getDate()) : now.getDate());}if (time) {if (date && time_link) result += ' ';let hours = now.getHours();if (hours.toString().length === 1) {hours = '0' + hours;}if (has_minutes) {let minutes = now.getMinutes();if (minutes.toString().length === 1) {minutes = '0' + minutes;}if (has_second) {let seconds = now.getSeconds();if (seconds.toString().length === 1) {seconds = '0' + seconds;}result += hours + time_link + minutes + time_link + seconds;} else {result += hours + ':' + minutes + time_link + '00';}} else {result += hours + time_link + '00' + time_link + '00';}}return result;}總結
以上是生活随笔為你收集整理的js时间戳转换日期,日期转换时间戳的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库架构中的基本概念
- 下一篇: 北理工上网linux版本,北理工路由器上