关注互联网应用及运维技术的个人博客

在react中引用JavaScript进行时间处理的方法总汇

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;
}
赞(0)
未经允许不得转载:飞天狒狒 » 在react中引用JavaScript进行时间处理的方法总汇

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址