日期格式轉毫秒值
var time = ‘2019-08-08 12:09:34’;
var time222 = time.replace("-", "/").replace("-", "/");
var timestamp = Date.parse(time222);
獲取當前月份以及近一年的月份
getMonths() {
var dataArr = [];
var data = new Date();
var year = data.getFullYear();
data.setMonth(data.getMonth() + 1, 1); //獲取到當前月份,設置月份
for (var i = 0; i < 12; i++) {
data.setMonth(data.getMonth() - 1); //每次循環一次 月份值減1
var m = data.getMonth() + 1;
m = m < 10 ? "0" + m : m;
dataArr.push(data.getFullYear() + "-" + m);
}
console.log(dataArr);
return dataArr;
},
毫秒值轉日期格式
formatDate:function(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
}
}
return fmt;
},
padLeftZero:function(str) {
return ('00' + str).substr(str.length);
},
獲取現在時刻的日期,星期幾,時分秒
getTime: function(type){
let date = new Date(),
currentDate,
currentTime,
seperator = "-", // 如果想要其他格式 只需 修改這里
year = date.getFullYear(),
month = date.getMonth() + 1,
weex = date.getDay(),
day = date.getDate(),
hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(),
minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(),
second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
month >= 1 && month <= 9 ? (month = "0" + month) : "";
day >= 0 && day <= 9 ? (day = "0" + day) : "";
//當前 日期
currentDate = year + seperator + month + seperator + day;
//當前 時間
currentTime = hour + ":" + minute + ":" + second;
// 輸出格式 為 2018-8-27 14:45:33
// console.log(currentDate +" "+ currentTime);
if(type==0){
return currentDate
}
else if(type==1){
return currentTime;
}
else if(type==2){
if(weex==1){
return '星期一'
}if(weex==2){
return '星期二'
}if(weex==3){
return '星期三'
}if(weex==4){
return '星期四'
}if(weex==5){
return '星期五'
}if(weex==6){
return '星期六'
}if(weex==7){
return '星期日'
}
}
else{
return currentDate+" "+ currentTime;
}
},
獲取當前時間戳
var time = (new Date()).getTime();
console.log('time=='+time);
兩個日期間隔天數
getDays: function(date1, date2) {
const dateTime = 1000 * 60 * 60 * 24; //每一天的毫秒數
const minusDays = Math.floor(((date2.getTime() - date1.getTime()) / dateTime));//計算出兩個日期的天數差
return Math.abs(minusDays);
},
var time = this.getDays(new Date('2019-08-09'),new Date('2019-07-09'));
console.log('time=='+time);
獲取10天后的日期
function daysAddSub(date, num) {
let d = new Date(date.getTime());
let newD = new Date(d.setDate(d.getDate() + num));//設置天數 +1 天
return formatDate(newD, 'yyyyMMdd');
}
console.log(this.daysAddSub(new Date('2018/06/28'),10));
獲取某一年的第一天的日期
_getFirstDayOfYear: function(date) {
var year = this._getYear(date);
var dateString = year + "-01-01 00:00:00";
return dateString;
},
_getYear: function(date) {
return this._transferDate(date).getFullYear();
},
某個日期是在當月中的星期幾
_getDayOfWeek: function(date1) {
return this._getWeek(date1);
},
/*獲取今天星期幾,如果為0代表星期日*/
_getWeek: function(date) {
return this._transferDate(date).getDay();
},
/*轉換日期*/
_transferDate: function(date) {
if(typeof date == "string") {
return new Date(date.replace(/-/ig, "/"));
} else {
return date;
}
},
某個日期是否晚於某個日期
_after: function(date1, date2) {
var stime = this._getTime(this._transferDate(date1));
var etime = this._getTime(this._transferDate(date2));
return stime < etime ? true : false;
},
/*返回 1970 年 1 月 1 日至今的毫秒數。*/
_getTime: function(date) {
return this._transferDate(date).getTime();
},
/*轉換日期*/
_transferDate: function(date) {
if(typeof date == "string") {
return new Date(date.replace(/-/ig, "/"));
} else {
return date;
}
},