读心悦

  • 读心随笔
  • 读心里话
  • 计算机
  1. 首页
  2. Javascript
  3. 正文

JavaScript时间处理方法总汇【在react中引用】

2020年08月26日 119点热度 0人点赞 0条评论
export const formatNumber = function (n) {
    n = n.toString();
    return n[1] ? n : '0' + n
}

/**
 * 时间戳和日期转换(年月日+时间)
 * @param times 
 */
export const formatDate = function (times) {
    console.log(times)
    var now = new Date(times)

    console.log(now)
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    return year + "-" + formatNumber(month) + "-" + formatNumber(date) + " " + formatNumber(hour) + ":" + formatNumber(minute) + ":" + formatNumber(second);
}
/**
 * 时间戳和日期转换(年月日)
 * @param {*} times 
 */
export const formatDate1 = function (times) {
    var now = new Date(times)
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var date = now.getDate();
    return year + "-" + formatNumber(month) + "-" + formatNumber(date);
}
/**
 *   时间戳转为 月日格式
 * @param {*} times 
 */
export const formatMonth = function (times) {
    var now = new Date(times)
    var month = now.getMonth() + 1;
    var date = now.getDate();
    return formatNumber(month) + "-" + formatNumber(date);
}
/**
 *  获取当月的天数
 */
export const mGetDate = function () {
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const days = new Date(year, month, 0);
    return days.getDate()
}
/**
 * 获取本周所有日期
 */
export const getWeekDay = function () {
    const date = new Date();
    const Monday = formatMonth(date.setDate(date.getDate() - date.getDay() + 1));
    const Tuesday = formatMonth(date.setDate(date.getDate() - date.getDay() + 2));
    const Wednesday = formatMonth(date.setDate(date.getDate() - date.getDay() + 3));
    const Thursday = formatMonth(date.setDate(date.getDate() - date.getDay() + 4));
    const Friday = formatMonth(date.setDate(date.getDate() - date.getDay() + 5));
    const Saturday = formatMonth(date.setDate(date.getDate() - date.getDay() + 6));
    const Sunday = formatMonth(date.setDate(date.getDate() - date.getDay() + 7));
    const weekDay = [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday];
    return weekDay
}
/**
 * 获取当月所有日期
 */
export const getNowM = function () {
    let now = new Date();
    let current_month_num = mGetDate();
    let current_month = [];
    for (let i = 1; i <= current_month_num; i++) {
        let day = now.setDate(i);
        let everyDay = formatMonth(day);
        current_month.push(everyDay);
    }
    return current_month;
}
赞微海报分享
本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: JavaScript
最后更新:2020年09月23日

读心悦

自己从事开发也有一段时间了,总有一些迷茫,对未来有一点恐惧,不知道以后会不会继续从事开发的岗位。无论未来做出怎样的选择,这个网站就记录一下从事开发这段时间的一些笔记、阅读笔记吧,好歹也给自己留个纪念吧,你说呢! 写点代码,读点书,读点心,读点自己!

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

读心悦

自己从事开发也有一段时间了,总有一些迷茫,对未来有一点恐惧,不知道以后会不会继续从事开发的岗位。无论未来做出怎样的选择,这个网站就记录一下从事开发这段时间的一些笔记、阅读笔记吧,好歹也给自己留个纪念吧,你说呢! 写点代码,读点书,读点心,读点自己!

标签聚合
CSS 阅读 node redux Nginx 随笔 flutter 闲谈 悦读 git Echarts mysql vue react 小程序 taro JavaScript canvas hook
推荐文章
  1. 小程序中picker底部弹出滚动选择器
  2. 微信小程序中获取当前日期
  3. JavaScript笔记
  4. antd-DatePicker组件获取时间值,以及相关设置
分类
  • flutter (11)
  • html/css (23)
  • Javascript (22)
  • Mysql (2)
  • node (2)
  • React (27)
  • vue (1)
  • 小程序 (41)
  • 悦读 (8)
  • 未分类 (2)
  • 读心里话 (9)

COPYRIGHT © 2020 读心悦

黔ICP备20005501号

黔公网安备52011502001078号