关于项目中使用时间的问题

项目中使用的时间

前端获取时间

获取JavaScript时间戳函数的5种方法,你都了解吗?\](https://zhuanlan.zhihu.com/p/652330526 前端获取本地时间戳 console.log(Date.now())//1642471441587 [时间戳在线转化](https://www.w3cschool.cn/tools/index?name=timestamptrans) Date.now()获取的是电脑本地 当电脑本地时间调整,或跟新不及时,获取错误时间。 举例: 电脑A启动vue项目 ,代码中使用了Date.now(); 电脑B打开访问vue项目。 当代码运行到Date.now()时,获取到的时是B电脑的时间。 ### 优化: JQuery获取服务器时间 $.ajax({ async: false, type: "GET", success: function(result, status, xhr) { var serverTime = new Date( xhr.getResponseHeader("Date")); serverTime = (new Date(serverTime)).getTime() / 1000; console.log(serverTime)//获取到软件所运行的服务器处获取的时间 }, error: function (a) { } }); 举例: 电脑A启动vue项目 ,代码中使用了Date.now(); 电脑B打开访问vue项目。 当代码运行到Date.now()时,获取到的时是A电脑的时间。 附:时间戳转常用时间格式 /** * @description 时间戳转常用时间格式 *@param {String} 时间戳 * @return 2020-11-9 14:21:35 格式时间 */ function filterTime(time) { var date = new Date(time) var Y = date.getFullYear() var M = date.getMonth() + 1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1 var D = date.getDate() var hours = date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours(); var minutes = (date.getMinutes() < 10 ? ("0" + date.getMinutes()) : date.getMinutes()); var seconds = (date.getSeconds() < 10 ? ("0" + date.getSeconds()) : date.getSeconds()); return `${Y}-${M}-${D} ${hours}:${minutes}:${seconds}` }

相关推荐
止观止21 分钟前
拥抱 ESNext:从 TC39 提案到生产环境中的现代 JS
开发语言·javascript·ecmascript·esnext
时寒的笔记32 分钟前
js逆向7_案例惠nong网
android·开发语言·javascript
吴声子夜歌1 小时前
ES6——Generator函数详解
前端·javascript·es6
吴声子夜歌1 小时前
ES6——Set和Map详解
前端·javascript·es6
xinzheng新政2 小时前
Javascript 深入学习基础·4
javascript·学习·servlet
粥里有勺糖2 小时前
视野修炼-技术周刊第129期 | 上一次古法编程是什么时候
前端·javascript·github
whuhewei2 小时前
JS获取CSS动画的旋转角度
前端·javascript·css
蓝黑20202 小时前
Vue组件通信之v-model
前端·javascript·vue
像素之间3 小时前
为什么运行时要加set NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve
前端·javascript·vue.js
M ? A3 小时前
Vue转React实战:defineProps精准迁移实战
前端·javascript·vue.js·经验分享·react.js·开源·vureact