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

项目中使用的时间

前端获取时间

获取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 3 中优雅地集成 Video.js 播放器:从组件封装到功能定制
开发语言·javascript·ecmascript
蓝冰凌7 小时前
Vue 3 中 defineExpose 的行为【defineExpose暴露ref变量】详解:自动解包、响应性与实际使用
前端·javascript·vue.js
奔跑的呱呱牛7 小时前
generate-route-vue基于文件系统的 Vue Router 动态路由生成工具
前端·javascript·vue.js
柳杉7 小时前
从动漫水面到赛博飞船:这位开发者的Three.js作品太惊艳了
前端·javascript·数据可视化
小彭努力中8 小时前
192.Vue3 + OpenLayers 实战:点击地图 Feature,列表自动滚动定位
vue·webgl·openlayers·geojson·webgis
TON_G-T8 小时前
day.js和 Moment.js
开发语言·javascript·ecmascript
Irene19919 小时前
JavaScript 中 this 指向总结和箭头函数的作用域说明(附:call / apply / bind 对比总结)
javascript·this·箭头函数
2501_921930839 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-appearance(更推荐自带的Appearance)
javascript·react native·react.js
还是大剑师兰特9 小时前
Vue3 中 computed(计算属性)完整使用指南
前端·javascript·vue.js