已知时间戳,转换成日期的函数
/**
* Created on 2018/7/3.
*
* @author LynnLin
* @class
*/
export default class GetDate {
static formatDateTime(timeStamp, timeStyle) { // timeStyle来控制显示方式
const date = new Date();
date.setTime(timeStamp);
const y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? (`0${m}`) : m;
let d = date.getDate();
d = d < 10 ? (`0${d}`) : d;
let h = date.getHours();
h = h < 10 ? (`0${h}`) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? (`0${minute}`) : minute;
second = second < 10 ? (`0${second}`) : second;
if (timeStyle === '1') {
return `${h}:${minute}`;
} else if (timeStyle === '2') {
return `${y}-${m}-${d}`;
} else {
return `${y}-${m}-${d} ${h}:${minute}:${second}`;
}
}
}
复制代码
获取时间长度
export function getTimeDistance(type) {
const now = new Date();
const oneDay = 1000 * 60 * 60 * 24;
if (type === 'today') {
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
return [moment(now), moment(now.getTime() + (oneDay - 1000))];
}
if (type === 'week') {
let day = now.getDay();
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
if (day === 0) {
day = 6;
} else {
day -= 1;
}
const beginTime = now.getTime() - (day * oneDay);
return [moment(beginTime), moment(beginTime + ((7 * oneDay) - 1000))];
}
if (type === 'month') {
const year = now.getFullYear();
const month = now.getMonth();
const nextDate = moment(now).add(1, 'months');
const nextYear = nextDate.year();
const nextMonth = nextDate.month();
return [moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`), moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000)];
}
if (type === 'year') {
const year = now.getFullYear();
return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
}
}
复制代码
JavaSript 常用的各种时间方法
getTime()
返回表示日期的毫秒数;与valueOf()方法返回的值相同 setTime(timestamp)
以毫秒数设置日期,会改变整个日期getFullYear()
取得4位数的年份(如2007而非仅07)getUTCFullYear()
返回UTC日期的4位数年份setFullYear(年)
设置日期的年份。传入的年份值必须是4位数字(如2007而非仅07)setUTCFullYear(年)
设置UTC日期的年份。传入的年份值必须是4位数字(如2007而非仅07)getMonth()
返回日期中的月份,其中0表示一月,11表示十二月getUTCMonth()
返回UTC日期中的月份,其中0表示一月,11表示十二月setMonth(月)
设置日期的月份。传入的月份值必须大于0,超过11则增加年份setUTCMonth(月)
设置UTC日期的月份。传入的月份值必须大于0,超过11则增加年份getDate()
返回日期月份中的天数(1到31)getUTCDate()
返回UTC日期月份中的天数(1到31)setDate(日)
设置日期月份中的天数。如果传入的值超过了该月中应有的天数,则增加月份setUTCDate(日)
设置UTC日期月份中的天数。如果传入的值超过了该月中应有的天数,则增加月份getDay()
返回日期中星期的星期几(其中0表示星期日,6表示星期六)getUTCDay()
返回UTC日期中星期的星期几(其中0表示星期日,6表示星期六)getHours()
返回日期中的小时数(0到23)getUTCHours()
返回UTC日期中的小时数(0到23)setHours(时)
设置日期中的小时数。传入的值超过了23则增加月份中的天数setUTCHours(时)
设置UTC日期中的小时数。传入的值超过了23则增加月份中的天数getMinutes()
返回日期中的分钟数(0到59)getUTCMinutes()
返回UTC日期中的分钟数(0到59)setMinutes(分)
设置日期中的分钟数。传入的值超过59则增加小时数setUTCMinutes(分)
设置UTC日期中的分钟数。传入的值超过59则增加小时数getSeconds()
返回日期中的秒数(0到59)getUTCSeconds()
返回UTC日期中的秒数(0到59)setSeconds(秒)
设置日期中的秒数。传入的值超过了59会增加分钟数setUTCSeconds(秒)
设置UTC日期中的秒数。传入的值超过了59会增加分钟数getMilliseconds()
返回日期中的毫秒数getUTCMilliseconds()
返回UTC日期中的毫秒数setMilliseconds(毫秒)
设置日期中的毫秒数setUTCMilliseconds(毫秒)
设置UTC日期中的毫秒数getTimezoneOffset()
返回本地时间与UTC时间相差的分钟数。例如,美国东部标准时间返回300。在某 地进入夏令时的情况下,这个值会有所变化