【vue 项目】格式化时间、获取当前时间、一小时前时间、一天前时间
接口请求参数需要传递时间
下面有三种方式获取当前时间
第一种
格式是最常见,采用三元判断添加0
created() {this.getCurrentTime()},methods: {getCurrentTime() {// 获取当前时间并打印var _this = thisconst yy = new Date().getFullYear()const mm = new Date().getMonth() + 1 < 10 ? '0' + new Date().getMonth() : new Date().getMonth()const dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()const hh = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()const mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()_this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ssconsole.log(_this.gettime)},}
第二种
获取当前时间和一小时前时间,但是格式需要调整,将时间格式化
created() {this.getCurrentTime()},methods: {getCurrentTime() {var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000);console.log(new Date(new Date().getTime() - 1 * 60 * 60 * 1000), new Date()) // 前一个小时 当前时间console.log(frontOneHour)},}
第三种
采用封装好的工具,将时间格式化
@/utils/index.js 中代码
/*** Parse the time to string* @param {(Object|string|number)} time* @param {string} cFormat* @returns {string | null}*/
export function parseTime(time, cFormat) {if (arguments.length === 0 || !time) {return null}const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'let dateif (typeof time === 'object') {date = time} else {if ((typeof time === 'string')) {if ((/^[0-9]+$/.test(time))) {// support "1548221490638"time = parseInt(time)} else {// support safari// https://stackoverflow.com/questions/4310953/invalid-date-in-safaritime = time.replace(new RegExp(/-/gm), '/')}}if ((typeof time === 'number') && (time.toString().length === 10)) {time = time * 1000}date = new Date(time)}const formatObj = {y: date.getFullYear(),m: date.getMonth() + 1,d: date.getDate(),h: date.getHours(),i: date.getMinutes(),s: date.getSeconds(),a: date.getDay()}const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {const value = formatObj[key]// Note: getDay() returns 0 on Sundayif (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }return value.toString().padStart(2, '0')})return time_str
}
在项目页面中引入
import { parseTime } from '@/utils/index'
JS
created() {this.getCurrentTime()},methods: {getCurrentTime() {var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000)console.log(new Date(new Date().getTime() - 1 * 60 * 60 * 1000), new Date()) // 前一个小时 当前时间console.log(frontOneHour)const CurrentTime = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')const fileName = parseTime(frontOneHour, '{y}-{m}-{d} {h}:{i}:{s}')console.log(CurrentTime)console.log(fileName)// var frontOneHour = new Date(new Date().getTime() - 1 * 60 * 60 * 1000) 一小时前// var frontOneDay = new Date(new Date().getTime() - 24 * 60 * 60 * 1000) 一天前// var frontSevenDay = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000) 七天前// const currentTime = parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}') 格式化当前时间// const frontOneHourTime = parseTime(frontOneHour, '{y}-{m}-{d} {h}:{i}:{s}') 格式化一小时前时间// const frontOneDayTime = parseTime(frontOneDay, '{y}-{m}-{d} {h}:{i}:{s}') 格式化一天前时间// const frontSevenDayTime = parseTime(frontSevenDay, '{y}-{m}-{d} {h}:{i}:{s}') 格式化七天前时间},}
【vue 项目】格式化时间、获取当前时间、一小时前时间、一天前时间相关推荐
- java获取时间戳(计算两个时间相差几天几小时几秒,获取当前日期是周几。。。)
java时间相关 博主所有博客都是验证过的 获取一个时间的时间戳 //获取当前时间戳Calendar cal1 = Calendar.getInstance();cal1.setTime(new Da ...
- 【前端】Vue项目:旅游App-博客总结
博客 链接 [前端]Vue项目:旅游App-(1)搭建项目.重置css.配置router和store(pinia) https://blog.csdn.net/karshey/article/deta ...
- 记一次vue项目打包优化
目录 前言 一.优化静态资源 二.vue项目打包体积优化 1.路由懒加载 2.抽离第三方js库 3.小结 三.代码优化 1.减少页面的重排和重绘 2.多设置key值 四.服务器优化 1.nginx服务 ...
- Vue项目 开启gzip
Vue项目 开启gzip Nginx 动态压缩与静态压缩,显著提高前后端分离项目响应速度 Gzip Precompression模块(Gzip Precompression) nginx缓存配置及开启 ...
- oracle向前推一个小时,oracle SQL里常用的时间函数,经典推荐
oracle SQL里常用的时间函数,经典推荐 (2009-03-11 23:16:54) 标签: 杂谈 常用日期型函数 1.Sysdate 当前日期和时间 SQL> Select sysdat ...
- vue 加载页面时触发时间_解析Vue项目的四个方面优化
在本篇文章里我们给大家整理了一篇关于优化VUE项目的四个总要点,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指正. 运行时优化 1.使用v-if代替v ...
- vue项目中动态显示时间
vue项目中动态显示时间 前言 一.js源代码 二.效果图 1.修改 2.html与style的修改 3.修改后效果图 总结 前言 在vue项目中动态显示时间,并且按日期.时间.星期一列排列. 我们想 ...
- 接手同事vue项目两个月的血和泪,关于vue项目性能优化,缩短首屏加载时间
最近接手了别人做的vue项目,项目跑起来后,有些页面很卡,首屏加载也慢,打包速度也慢.于是,研究了很久vue的项目性能优化,下面我将从两个部分来详解vue项目的性能优化: 代码优化 webpack打包 ...
- android 字符串 时间格式化,Android 获取年月日时分秒 格式化指定时间字符串
1.获取Android系统当前时间年月日 时分秒(24小时制) //获取当前时间 Time time=new Time(); time.setToNow(); Log.i("cp" ...
- 【Vue-router中,require代替import解决vue项目首页加载时间过久】
vue-router中,require代替import解决vue项目首页加载时间过久的问题 - 雨打芭蕉wt - 博客园
最新文章
- 陌陌安全开源了 Java 静态代码安全审计插件
- python xlrd处理表格常用方法
- 【网络安全】文件上传绕过思路总结
- java的rserve实现_Java 使用 Rserve 实现与 R 的通信
- python画两条曲线_查找在matplotlib中绘制的两条曲线之间的区域(在区域之间填充)...
- tf.nn.sigmoid_cross_entropy_with_logits 分类
- 创建shap文件的属性字段类型区别_shapefile数据文件组成
- Spring实战第五版(中文版)学习笔记-第一章 Spring起步
- 2021年王道数据结构课后题
- 机器学习之One-Hot Encoding详解
- 在条形码上如何添加日期
- Android RSA 加密
- 业聚医疗通过聆讯:上半年营收6885万美元 钱永勋为实控人
- 《操作系统》期末考试试卷2
- 卡通幼儿英语数学拼音早教机 官网
- 干货(附源代码) | 爬取一万条b站评论,分析9.7分的新番凭啥这么火?
- CSDN PC端网站及其部分功能产品体验的一点分析
- python gui学生管理系统(Python gui+MySQL数据库)
- 《C++》第1章——第111章
- 论文中公式居中编号右对齐以及左下角基金的插入方法