js时间戳日期转换实用工具

您所在的位置:网站首页 时间戳转换代码 js时间戳日期转换实用工具

js时间戳日期转换实用工具

2023-03-27 09:19| 来源: 网络整理| 查看: 265

发个之前写的日期时间戳转换实用工具库,写这个工具的原因是因为之前工作的时候经常需要写时间戳转换,因为无法保证后端给的数据格式一定是自己想要的,所以作为前端经常都得自己写时间转换,索性就统一把经常见到的时间转换需求整理了一下写成了小工具,省下重复写代码的功夫。我的原则是前端能做的事就前端来解决,有功夫跟后端扯皮早就自己把东西做完了。大前端时代,前端能做到大部分以前后端才能做的事情,没有路就自己开路,能解决问题就好,无所谓前后端之争。

/** * 这是简便的时间转换工具库。 ** 使用方法 * import datetransform from "common/datetransform.js"; * 引入后可通过datetransform.gethour(timestamp)等方法直接使用 * * 写了几个常见的方法 : * toTformat(times) // 将日期转换成含T的格式不含秒 如2018-08-08T11:11 * tonormalformat(times) // 将日期转换成标准格式 * tostamp(times)日期转为时间戳 * tozerostamp(times)日期转时间戳(零点) * getall(timestamp)时间戳转日期(包含年月日时分秒) * getday(timestamp)时间戳转日期(包含年月日) * gethour(timestamp)时间戳转日期(包含时分秒) * getnow(type)时间戳转日期 * // 当前日期/时分秒 * type为"day"时返回当天日期(包含年月日) * type为"hour"时返回当天时间(包含时分秒) * type为空时返回当天日期加时间(包含年月日时分秒) * **/ let datetransform = { // 将日期转换成含T的格式不含秒 toTformat(times){ var time=times if(time){ var date = new Date(time); let Y = date.getFullYear() + '-' let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) return Y + M + D + "T" + h + m }else{ return "" } }, // 将日期转换成标准格式 tonormalformat(times){ var time=times if(time){ var date = new Date(time); let Y = date.getFullYear() + '-' let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) return Y + M + D + " " + h + m + s }else{ return "" } }, // 日期转时间戳 tostamp(times) { if(times){ let time = new Date(times) return time.getTime() }else{ return "" } }, // 日期转时间戳(零点) tozerostamp(times) { if(times){ let time = new Date(times + " 00:00:00") return time.getTime() }else{ return "" } }, // 时间戳转日期(包含年月日时分秒) getall(timestamp) { if(timestamp){ let date = new Date(parseInt(timestamp)) let Y = date.getFullYear() + '-' let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()); return Y + M + D + " " + h + m + s }else{ return "" } }, // 时间戳转日期(包含年月日) getday(timestamp) { if(timestamp){ let date = new Date(parseInt(timestamp)) let Y = date.getFullYear() + '-' let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()); return Y + M + D }else{ return "" } }, // 时间戳转日期(包含时分秒) gethour(timestamp) { if(timestamp){ let date = new Date(parseInt(timestamp)) let Y = date.getFullYear() + '-' let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()) return h + m + s; }else{ return "" } }, // 日期/时分秒,参数为空时返回当前日期时间 type为"day"时返回当天日期 type为"hour"时返回当天时间 type空时返回当天日期加时间 getnow(type) { let date = new Date() let Y = date.getFullYear() + '-' let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()); let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':' let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':' let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()); if (type == "day") { return Y + M + D } else if (type == "hour") { return h + m + s; } else { return Y + M + D + " " + h + m + s } }, } export default datetransform 复制代码

本文由mdnice多平台发布



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3