小程序时间格式大全

Posted liqianglog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小程序时间格式大全相关的知识,希望对你有一定的参考价值。

小程序时间格式大全 util

博客推荐
下面文章方面比较全面,可以看看:
JavaScript Date(日期)对象

const formatTime = date => 
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')

//获取当前日期,以“/”连接
const formatDate = date => 
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join('/')

//获取当前日期,以“年月日”连接
const formatDateCH = date => 
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return year + "年" + month + "月" + day + "日"

//获取当前日期,以“-”连接
const formatDateByH = date => 
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join('-')

//将string格式日期转换为“/”连接只包含月日的日期
const formatDateToSimple = data => 
    var date = new Date(Date.parse(data));
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [month, day].join('/')

//获取string格式日期的星期
const formatDateToWeek = data => 
    var date = new Date(Date.parse(data));
    const month = date.getDay();
    var weekDay ;
    switch (month)
        case 0:
            weekDay = '周日';
            break;
        case 1:
            weekDay = '周一';
            break;
        case 2:
            weekDay = '周二';
            break;
        case 3:
            weekDay = '周三';
            break;
        case 4:
            weekDay = '周四';
            break;
        case 5:
            weekDay = '周五';
            break;
        case 6:
            weekDay = '周六';
            break
    
    return weekDay;


//日期的加减
const addDay = data => 
    //下面的不是时间戳,是时间戳*1000
    var timestamp = Date.parse(new Date());
    var newTimestamp = timestamp + data * 24 * 60 * 60 * 1000;
    var date = new Date(newTimestamp);

    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();

    return [year, month, day].map(formatNumber).join('-');


//月份的加减
const addMonth = num => 
    if(typeof num == "string")
        num = parseInt(num);
    
    var date = new Date();
    const curYear = date.getFullYear();
    const curMonth = date.getMonth() + 1;
    const curDay = date.getDate();
    let month = (curMonth + num - 1) % 12;
    let year = curYear + (curMonth + num - month)/12;
    let days = curDay;
    date = new Date(year, month, days);
    year = date.getFullYear();
    month = date.getMonth() + 1;
    const day = date.getDate();

    return [year, month, day].map(formatNumber).join('-')

//月份第几天增加后获取月份的第几天
const getDayByAddDay = data => 
    //下面的不是时间戳,是时间戳*1000
    var timestamp = Date.parse(new Date());
    var newTimestamp = timestamp + data * 24 * 60 * 60 * 1000;
    var date = new Date(newTimestamp);

    return date.getDate();


const formatNumber = n => 
    n = n.toString()
    return n[1] ? n : '0' + n


module.exports = 
    formatTime: formatTime,
    formatDate: formatDate,
    formatDateByH: formatDateByH,
    formatDateCH: formatDateCH,
    addDay: addDay,
    addMonth: addMonth,
    getDayByAddDay: getDayByAddDay,
    formatDateToSimple: formatDateToSimple,
    formatDateToWeek: formatDateToWeek,



以上是关于小程序时间格式大全的主要内容,如果未能解决你的问题,请参考以下文章

C#时间日期格式大全

Java时间格式转换大全

dede调用时间大全标签,不同格式!

Sql日期时间格式转换大全

小程序爬坑之时间格式IOS的兼容

小程序获取时间格式