JS-Date 日期处理

日期相关操作记录

毫秒数转标准格式日期
function formatterTime(times){
    let date = new Date(times);
    var year = date.getFullYear();            // 年份
    var month = date.getMonth() + 1;        // 月份
    var strDate = date.getDate();        // 一月中的某一日
    var hours = date.getHours(),           // 小时
        minutes = date.getMinutes(),         // 分钟
        seconds = date.getSeconds();       // 秒数
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    if (hours >= 0 && hours <= 9) {
        hours = "0" + hours;
    }
    if (minutes >= 0 && minutes <= 9) {
        minutes = "0" + minutes;
    }
    if (seconds >= 0 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    let currentDate = year + "-" + month + "-" + strDate + " " +
        hours + ":" + minutes + ":" + seconds;

    return currentDate;
}

还有获取星期几的方法getDay()

let weekDays = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
let current = new Date();
weekDays[current.getDay()];        // "星期日"
对当前日期进行操作
let current= new Date();
// 获取当前日期时间 格式  yyyy/MM/dd
let currentDate = current.toLocaleDateString();
// 获取当前时间 格式  hh:mm:ss GMT+0800 (中国标注时间)
let currentDay = current.toTimeString();

// 格式处理一下 输出 yyyy-MM-dd hh:mm:ss
currentDate.replace(/\//g,"-")+" "+currentDay.split(" ")[0];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

heroboyluck

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

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

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

打赏作者

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

抵扣说明:

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

余额充值