借助dayjs(一个轻量级的处理时间和日期的JavaScript库)
API中文文档GitHub地址
API转载
安装:
NPM:
npm install dayjs --save
import dayjs from 'dayjs'
// 或者 CommonJS
// var dayjs = require('dayjs');
dayjs().format(); //2020-01-06T10:15:01+08:00
CDN:
<!-- 最新的压缩后的 JavaScript 文件 -->
<script src="https://unpkg.com/dayjs"></script>
<script>
dayjs().format();
</script>
实现
var dayjs = require('dayjs');
//当前年的最开始
// console.log( dayjs().startOf('year').$d.valueOf());
let yearStar = dayjs().startOf('year').$d.valueOf();
//当前月的最开始
// console.log(dayjs().startOf('month').$d.valueOf());
let monthStar = dayjs().startOf('month').$d.valueOf();
//当前周的最开始
// console.log(dayjs().startOf('week').$d.valueOf());
let weekStar = dayjs().startOf('week').$d.valueOf();
//当前天的最开始
// console.log(dayjs().startOf('day').$d.valueOf());
let todayStar = dayjs().startOf('day').$d.valueOf();
//昨天的最开始
let yestodayStar = dayjs().startOf('day').$d.valueOf() - 1000*60*60*24;
let currenttime;
let ss = 1577145600000;
//超出今年的年份
if(ss < yearStar){
//本身所在的年份 let ss = 1525492800000;
console.log('本身所在的年份');
let month = dayjs(ss).month()+1
let date = dayjs(ss).date()
month > 10 ? month = month : month = '0'+ month
date > 10 ? date = date : date = '0'+ date
currenttime = `${dayjs(ss).year()}-${month}-${date}`
}
//在今年不在本月中
if(ss > yearStar && ss < monthStar ){
//当前年的日期 let ss = 1571760000000;
console.log('当前年的日期');
let month = dayjs(ss).month()+1
let date = dayjs(ss).date()
month > 10 ? month = month : month = '0'+ month
date > 10 ? date = date : date = '0'+ date
currenttime = `${dayjs(ss).year()}-${month}-${date}`
}
//在本月不在本周
if(ss > monthStar && ss < weekStar ){
//当前月的日期 let ss = 1575302400000;
console.log('当前月的日期');
let month = dayjs(ss).month()+1
let date = dayjs(ss).date()
month > 10 ? month = month : month = '0'+ month
date > 10 ? date = date : date = '0'+ date
currenttime = `${month}-${date}`
}
//在本周不在昨天
if(ss > weekStar && ss < yestodayStar){
//周几 let ss = 1576987200000;
console.log('周几');
let weekObj = {0:'日',1:'一',2:'二',3:'三',4:'四',5:'五',6:'六'};
let week = dayjs(ss).day()
let weekValue = weekObj[week];
let hour = dayjs(ss).hour()
let minute = dayjs(ss).minute()
hour > 10 ? hour = hour : hour = '0'+ hour
minute > 10 ? minute = minute : minute = '0'+ minute
currenttime = `周${weekValue}-${hour}:${minute}`
}
//昨天的
if( ss > yestodayStar && ss < todayStar) {
//昨天 let ss = 1577073600000;
console.log('昨天');
let hour = dayjs(ss).hour()
let minute = dayjs(ss).minute()
hour > 10 ? hour = hour : hour = '0'+ hour
minute > 10 ? minute = minute : minute = '0'+ minute
currenttime = `昨天-${hour}:${minute}`
}
//当天的
if(ss > todayStar){
//时 + 分 let ss = 1577145600000;
console.log('今天');
let hour = dayjs(ss).hour()
let minute = dayjs(ss).minute()
hour > 10 ? hour = hour : hour = '0'+ hour
minute > 10 ? minute = minute : minute = '0'+ minute
currenttime = `${hour}:${minute}`
}
console.log(currenttime);
//十位时间戳转换日期格式
export const TimeStamp = (shijianchuo) => {
function add0(m) { return m < 10 ? '0' + m : m }
let time = new Date(shijianchuo * 1000);
let y = time.getFullYear();
let m = time.getMonth() + 1;
let d = time.getDate();
let h = time.getHours();
let mm = time.getMinutes();
let s = time.getSeconds();
return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}
//时间差值转换为时间格式(倒计时,十位时间戳)
export const timeDate = (shijianchuo) => {
function add0(m) { return m < 10 ? '0' + m : m }
var year = add0(Math.floor(shijianchuo / 86400 / 365));
shijianchuo = shijianchuo % (86400 * 365);
var month = add0(Math.floor(shijianchuo / 86400 / 30));
shijianchuo = shijianchuo % (86400 * 30);
var day = add0(Math.floor(shijianchuo / 86400));
shijianchuo = shijianchuo % 86400;
var hour = add0(Math.floor(shijianchuo / 3600));
shijianchuo = shijianchuo % 3600;
var minute = add0(Math.floor(shijianchuo / 60));
shijianchuo = shijianchuo % 60;
var second = add0(shijianchuo);
let obj = {}
return obj = {
year,
month,
day,
hour,
minute,
second,
}
}
//保留两位小数
export const GetFloat= (x) => {
if (x != '.'){
var f = Math.round(x * 100) / 100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs <= 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}else{
return '0.00';
}
}
///或者使用
number.toFixed(2);
//异或加密
export function Exclusive(value) {
let pass = 1;
let x = ff(pass, value)
function ff(pass, value) {
let last = ''
for (let i = 0; i < value.length; i++) {
let text2 = ''
text2 = value.charCodeAt(i) ^ pass
last += String.fromCharCode(text2)
}
return last
}
return x
}