export const getTime = function() {
//获取当前签到时间 : 12:00
let date = new Date(),hour = date.getHours()>=10?date.getHours():'0'+date.getHours(),minute = date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes();
return hour+':'+minute
};
export const getDate = function(y="年",m="月",d="日") {
//获取当前签到日期 : 2019年10月31日
let date = new Date(),year = date.getFullYear(),month = (date.getMonth()+1)>=10?date.getMonth()+1:'0'+(date.getMonth()+1),day=date.getDate()>=10?date.getDate():'0'+date.getDate();
return year+y+month+m+day+d
};
export const getWeek = function() {
//获取今天是星期几
let arr = ["日", "一", "二", "三", "四", "五", "六"],week = new Date().getDay();
return arr[week]
};
export const getYesterday = function(){
// 获取昨天日期
let day = new Date();
day.setTime(day.getTime()-24*60*60*1000);
return day.getFullYear()+"-" + ((day.getMonth()+1)>=10?(day.getMonth()+1):'0'+(day.getMonth()+1)) + "-" + (day.getDate()>=10?day.getDate():'0'+day.getDate())
}
export const timestampToTime =function(timestamp){
//利用时间戳获取时间
let date = new Date(timestamp)
let h = date.getHours()>=10?date.getHours():'0'+date.getHours();
let m = date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes();
return h + ':' + m
}
export const timestampToDate = function(timestamp){
//利用时间戳获取日期
let date = new Date(timestamp)
let year=date.getFullYear();
let mon = (date.getMonth()+1)>=10?date.getMonth()+1:'0'+(date.getMonth()+1);
let day = date.getDate()>=10?date.getDate():'0'+date.getDate();
let h = date.getHours()>=10?date.getHours():'0'+date.getHours();
let m = date.getMinutes()>=10?date.getMinutes():'0'+date.getMinutes();
return year+'-'+mon+'-'+day +' '+ h + ':' + m
}
javascript常用时间格式化方法(星期、日期、时间)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...