获取自定义年月日时分秒格式时间并支持获取当天、前天、后天

格式化时间戳

1.项目引入getdateTime.js (vue项目示例)

/**
 * @param {String} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
 */


function dateTimeStr(str){
	var date = new Date(),
	year = date.getFullYear(), //年
	month = date.getMonth() + 1, //月
	day = date.getDate(), //日
	hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
	minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
	second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	hour >= 0 && hour <= 9 ? hour : "";
	minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
	second >= 0 && second <= 9 ? (second = "0" + second) : "";
	if(str.indexOf('y') != -1){
		str = str.replace('y', year)
	}
	if(str.indexOf('m') != -1){
		str = str.replace('m', month)
	}
	if(str.indexOf('d') != -1){
		str = str.replace('d', day)
	}
	if(str.indexOf('h') != -1){
		str = str.replace('h', hour)
	}
	if(str.indexOf('i') != -1){
		str = str.replace('i', minute)
	}
	if(str.indexOf('s') != -1){
		str = str.replace('s', second)
	}
	return str;
}
 
 //获取日期的今天,明天,后天 -1是昨天,0是今天,1是后一天
 /**
  * @param {String} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
  */
function getDateStr(today, addDayCount,str) {
     let date;
     if (today) {
       date = new Date(today);
     } else {
       date = new Date();
     }
     date.setDate(date.getDate() + addDayCount); //获取AddDayCount天后的日期 
	 var year = date.getFullYear(), //年
	 month = date.getMonth() + 1, //月
	 day = date.getDate(), //日
	 hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(), //时
	 minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(), //分
	 second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
	 month >= 1 && month <= 9 ? (month = "0" + month) : "";
	 day >= 0 && day <= 9 ? (day = "0" + day) : "";
	 hour >= 0 && hour <= 9 ? hour : "";
	 minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
	 second >= 0 && second <= 9 ? (second = "0" + second) : "";
	 if(str.indexOf('y') != -1){
	 	str = str.replace('y', year)
	 }
	 if(str.indexOf('m') != -1){
	 	str = str.replace('m', month)
	 }
	 if(str.indexOf('d') != -1){
	 	str = str.replace('d', day)
	 }
	 if(str.indexOf('h') != -1){
	 	str = str.replace('h', hour)
	 }
	 if(str.indexOf('i') != -1){
	 	str = str.replace('i', minute)
	 }
	 if(str.indexOf('s') != -1){
	 	str = str.replace('s', second)
	 }
	 return str;
}
module.exports = {
	dateTimeStr: dateTimeStr,
	getDateStr:getDateStr
}

2.项目引用并调用js方法
引入

  import getDateTime from '../../../static/js/getdateTime.js';

调用

// 渲染默认起止时间
this.startTime = getDateTime.dateTimeStr('y-m-d h:i:s'); //今天 2023-03-01 15:39:21
this.endTime = getDateTime.getDateStr(null, 1,'y-m-d h:i:s'); //明天  2023-03-02 15:39:21
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值