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

项目中使用的时间

前端获取时间

获取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}` }

相关推荐
利刃大大7 小时前
【Vue】Vue2 和 Vue3 的区别
前端·javascript·vue.js
Lhuu(重开版7 小时前
JS:正则表达式和作用域
开发语言·javascript·正则表达式
yuguo.im8 小时前
我开源了一个 GrapesJS 插件
前端·javascript·开源·grapesjs
安且惜8 小时前
带弹窗的页面--以表格形式展示
前端·javascript·vue.js
摘星编程10 小时前
用React Native开发OpenHarmony应用:NFC读取标签数据
javascript·react native·react.js
AGMTI11 小时前
webSock动态注册消息回调函数功能实现
开发语言·前端·javascript
不吃香菜的猪12 小时前
使用@vue-office/pdf时,pdf展示不全
javascript·vue.js·pdf
wuhen_n12 小时前
TypeScript的对象类型:interface vs type
前端·javascript·typescript
css趣多多12 小时前
props,data函数,computed执行顺序
前端·javascript·vue.js
一个不称职的程序猿12 小时前
构建优雅的 Vue.js 表情包选择器:一个功能丰富且可定制的 Emoji Picker 组件
前端·javascript·vue.js