前端关于时间的常用方法
1.获取当前时间
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
if(month >= 1 && month <= 9) {
month = "0" + month;
}
if(strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = year + seperator1 + month + seperator1 + strDate;
//var currentdate = year + "年" + month + "月" + strDate +"日";
return currentdate;
}
2.比较时间大小
//如果time2大于time1 返回true 否则 返回false
function compareTime(time1, time2) {
if(time_to_sec(time2) - time_to_sec(time1) > 0) {
return true;
}
return false;
}
//将时分秒转为时间戳
function time_to_sec(time) {
if(time !== null) {
var s = "";
var hour = time.split(":")[0];
var min = time.split(":")[1];
var sec = time.split(":")[2];
s = Number(hour * 3600) + Number(min * 60) + Number(sec);
return s;
}
}
3.获取时间的时分秒
var time="2021-05-24";
var hour = time.split(':')[0];
var min = time.split(':')[1];
var sec = time.split(':')[2];
4.yyyy年MM月dd日转换为yyyy-MM-dd
function restr(str4 ) {
if (/.*[\u4e00-\u9fa5]+.*$/.test(str4)) {
//var str1 = strh.replace(/[^bai\d]/g,'-').substr(0, strh.length - 1);
var str2 = strh.replace(/[^bai\d]/g, '-');
var str3 = str2.substr(str2.length - 1, 1);
if (str3 == "-") {
str4 = str2.substr(0, strh.length - 1);
}
}
return str4;
}
var s="2021-03汇总";
s=s.substring(0,s.length-2).replace("-","年")+"月";
//输出:2021年03月
5.年月日时分秒带星期
function date() {
var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()];
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var currentTime1 = this.year + "年" + this.month + "月" + this.date + "日 "
+this.day
+this.hour+"时"+this.minute+"分"+this.second;
alert(currentTime1);
}
$(function(){
var date1 = new Date();
var date2=getDate("2020-01-02");
console.info(date2);
//7天后的日期
addNDays(date2,7);
});
//时间后多少天
function addNDays(d,n){
var time=d.getTime();
var newTime=time+n*24*60*60*1000;
var date=new Date(newTime)
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
if (this.month >= 1 && this.month <= 9) {
this.month = "0" + this.month;
}
if (this.date >= 0 && this.date <= 9) {
this.date = "0" + this.date;
}
var currentTime1 = this.year + "-" + this.month + "-" + this.date + " "
console.info(currentTime1);
return currentTime1;
};
//字符串转日期
function getDate(strDate) {
var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
function(a) {
return parseInt(a, 10) - 1;
}).match(/\d+/g) + ')');
return date;
}
6.字符串时间转化为文字
function dateMax(str){
let chinese=['〇','一','二','三','四','五','六','七','八','九','十'];
let numStr=str.replace(/[^0-9]+/g, '');
let year=chinese[numStr[0]]+chinese[numStr[1]]+chinese[numStr[2]]+chinese[numStr[3]];
let month=numStr[4]==='0'?chinese[numStr[5]]:chinese[10]+chinese[numStr[5]];
let day='';
if (numStr[6] === '0') {
day = chinese[numStr[7]];
} else if (numStr[6] === '1') {
if (numStr[7] === '0') {
day = chinese[10];
} else {
day = chinese[10] + chinese[numStr[7]];
}
} else if (numStr[6] === '2' || numStr[6] === '3') {
if (numStr[7] === '0') {
day = chinese[numStr[6]] + chinese[10];
} else {
day = chinese[numStr[6]] + chinese[10] + chinese[numStr[7]];
}
}
return year+'年'+month+'月'+day+'日';
}