JavaScript Date(时间日期对象);中国时间转YYYY-MM-DD格式; 多个日期中取获得最大和最小日期

创建Date对象
new Date('2019/08/16 9:51:30');
new Date(2019,8,16,9,50,30);    //第二个参数代表月份,月份的范围:0-11,代表1-12月
new Date()    //存储当前所在的操作系统时间
new Date(1000*60*60*24);    //距离计算机元年毫秒数对应的日期时间

获取Date对象中存储的日期时间
var e=new Date();
console.log( e.getFullYear() );    // 获取年份
console.log( e.getMonth() );    // 获取月份
console.log( e.getDate() );    // 获取日期
console.log( e.getHours() );    // 获取时
console.log( e.getMinutes() );    // 获取分
console.log( e.getSeconds() );    // 获取秒
console.log( e.getMilliseconds() );    // 获取毫秒
console.log( e.getDay() );    // 获取星期
console.log( e.getTime() );    // 获取当前时间距离计算机元年毫秒数

设置日期时间
var da=new Date('2019/8/16 14:22:30');
da.setFullYear(2017);    // 修改年份
da.setMonth(11);    // 修改月份
da.setDate(7);    // 修改天
da.setDate( da.getDate()+7 );    // 天数修改到七天后
da.setHours(5);    // 修改小时
da.setHours( da.getHours()+3 );    // 小时设置到3小时以后
da.setMinutes(55);    // 修改分钟
da.setSeconds(00);    // 修改秒
console.log( da.toLocaleString() );    // 2017/12/14 上午8:55:00

拷贝一个Date对象
var d1=new Date();
var d2=new Date(d1);     //以参数形式传递给Date,就会拷贝成一个新的对象

获取当前系统时间,精确到秒
let yy = new Date().getFullYear();
let mm = new Date().getMonth() + 1 < 10 ? "0" + (new Date().getMonth() + 1) : new Date().getMonth() + 1;
let dd = new Date().getDate() < 10 ? "0" + new Date().getDate() : new Date().getDate();
let hh = new Date().getHours() < 10 ? "0" + new Date().getHours() : new Date().getHours();
let mf = new Date().getMinutes() < 10 ? "0" + new Date().getMinutes() : new Date().getMinutes();
let hm = new Date().getSeconds() < 10 ? "0" + new Date().getSeconds() : new Date().getSeconds();
    
let dateTime = yy + "." + mm + "." + dd + " " + hh + ":" + mf + ":" + hm
console.log( dateTime )

中国时间转YYYY-MM-DD格式

function formatDate (date) {         // 时间格式处理, 返回值:2011-06-25
    var y = date.getFullYear();

    var m = date.getMonth() + 1;
    m = m < 10 ? '0' + m : m;

    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;

    return y + '-' + m + '-' + d;
}

function formatDateTime (date) {        // 时间格式处理, 返回值:2011-06-28 00:00:00
    var y = date.getFullYear();

    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;

    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;

    var h = date.getHours(); 
    h = h < 10 ? ('0' + h) : h;

    var min = date.getMinutes(); 
    min = min < 10 ? ('0' + min) : min; 

    var s = date.getSeconds();
    s = s < 10 ? ('0' + s) : s;

    return y + '-' + m + '-' + d +' '+ h +':'+ min + ':' + s;
}

多个日期中取获得最大和最小日期 

var dates=[];
dates.push(new Date("2011/06/25"))
dates.push(new Date("2011/06/26"))
dates.push(new Date("2011/06/27"))
dates.push(new Date("2011/06/28"))

var maxDate=new Date(Math.max.apply(null,dates));
var minDate=new Date(Math.min.apply(null,dates));

console.log(minDate);    // Sat Jun 25 2011 00:00:00 GMT+0800 (中国标准时间)
console.log(maxDate);    // Tue Jun 28 2011 00:00:00 GMT+0800 (中国标准时间)

获取从指定年份到今年之间的所有年份

let startYear = '2018';    // 从哪一年开始
let date = new Date;
let nowYears = date.getFullYear();
let Years = nowYears - startYear;
let yearArr = [];
for (let i = 0; i <= Years; i++) {
    yearArr.push(nowYears--)
}
console.log(yearArr);

时间戳转换成通用时间格式

/**
 * 获取各种格式日期
 * msec   ---   时间戳
 * dateFormat   ---   需要的格式, yyyy-mm-dd hh:ff:ss, 传什么返回什么, 不传全部返回
 * */
function commonMsToDate(msec, dateFormat) {
  !dateFormat ? dateFormat = 'yyyy-mm-dd hh:mm:ss' : dateFormat;
  let datetime = new Date(msec);
  let year = datetime.getFullYear();
  let month = datetime.getMonth();
  let date = datetime.getDate();
  let hour = datetime.getHours();
  let minute = datetime.getMinutes();
  let second = datetime.getSeconds();

  if (dateFormat.includes('yyyy')) {
    dateFormat = dateFormat.replace('yyyy', year)
  }
  if (dateFormat.includes('mm')) {
    dateFormat = dateFormat.replace('mm', (month + 1) >= 10 ? (month + 1) : '0' + (month + 1))
  }
  if (dateFormat.includes('dd')) {
    dateFormat = dateFormat.replace('dd', (date + 1) < 10 ? '0' + date : date)
  }
  if (dateFormat.includes('hh')) {
    dateFormat = dateFormat.replace('hh', (hour + 1) < 10 ? '0' + hour : hour)
  }
  if (dateFormat.includes('ff')) {
    dateFormat = dateFormat.replace('ff', (minute + 1) < 10 ? '0' + minute : minute)
  }
  if (dateFormat.includes('ss')) {
    dateFormat = dateFormat.replace('ss', (second + 1) < 10 ? '0' + second : second)
  }

  return dateFormat;
}


// commonMsToDate(1635848325981,'hh:ff')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Web Erek

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值