zoukankan      html  css  js  c++  java
  • 以一个日期为中心点,生成前后一周的数据

    根据一个需求,需要做一个功能:
    根据传入的一个json(日期,年份,月数,星期),来生成以此为中心点的前后一周的数据。
    该版本的JS 暂时不知处无参数传入,后续待优化。
    PS:如图,红圈标记出来就是我们传入的值,上下为生成的对应数据


    JS完整代码如下:

    // 需要传入的json 格式
    var json = {
    year: 2017,
    month: 12,
    weekday: '周六',
    day: 1
    }
    var calendar = {
    // 传入任意一个数,生成以该数为中心的,前后三天(共七天的数组)
    // 支持年,月,日,星期 四个数据的输出
    montharr: [], //月份数组
    arr: [], //存储前后七天的数组
    calendar_sign: 0,
    init: function () {
    calendar.arr[3] = json;
    calendar.arr.length = 7;
    calendar.judgeDay(json); //模拟json 数据,
    console.log(calendar.arr)

    },
    // 判断星期
    judgeWeek: function (week) {
    var weekday = '';
    if (typeof week == 'string') {
    switch (week) {
    case '周日':
    weekday = 0;
    break;
    case '周一':
    weekday = 1;
    break;
    case '周二':
    weekday = 2;
    break;
    case '周三':
    weekday = 3;
    break;
    case '周四':
    weekday = 4;
    break;
    case '周五':
    weekday = 5;
    break;
    case '周六':
    weekday = 6;
    break;
    default :
    break;
    }
    return Number(weekday);
    }
    else {
    switch (week) {
    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;
    default :
    break;
    }
    return weekday;
    }

    },
    judgeDay: function (json) {
    calendar.judgeMonth(json.year);
    var add = 0, reduce = 0;
    for (var i = 0; i < 7; i++) {
    var temJson = {};
    var weekNum=calendar.judgeWeek(json.weekday);
    if (i == 3) {
    calendar.arr[3].sign=0;
    } else {
    // 判断中心日期 前后日年月星期数据
    if (i < 3) {
    temJson.day = json.day - (i + 1);
    temJson.year = json.year;
    if (temJson.day < 1) {
    temJson.month = json.month - 1;
    if (temJson.month < 1) {
    temJson.year = json.year - 1;
    temJson.month = 12;
    }
    temJson.sign = calendar.calendar_sign - 1;
    reduce++;
    temJson.day = calendar.montharr[temJson.month - 1] - reduce + 1;
    } else {
    temJson.month = json.month;
    temJson.sign = calendar.calendar_sign;
    }
    var weekday=calendar.judgeWeek(Math.abs(weekNum-(i+1)+7)%7);
    temJson.weekday=weekday;
    calendar.arr[3 - i - 1] = temJson;
    } else if (i > 3) {
    temJson.day = json.day + (i - 3);
    temJson.year = json.year;
    if (temJson.day > calendar.montharr[json.month - 1]) {
    temJson.month = json.month + 1;
    if (temJson.month > 12) {
    temJson.month = 1;
    temJson.year = json.year + 1;
    }
    temJson.sign = calendar.calendar_sign + 1;
    add++;
    temJson.day = add;
    } else {
    temJson.month = json.month;
    temJson.sign = calendar.calendar_sign

    }
    var weekday=calendar.judgeWeek((weekNum+(i-3))%7);
    temJson.weekday=weekday;
    calendar.arr[3 + i - 3] = temJson;
    }

    }

    }

    },
    //判断每月天数
    judgeMonth: function (year) {
    if (calendar.judgeYear(year)) {
    calendar.montharr = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
    calendar.montharr = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    }
    },

    //判断闰年
    judgeYear: function (year) {
    return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)
    }

    }
  • 相关阅读:
    5.22 css和基本选择器
    5.21http网页基础
    ArrayList类源码浅析(二)
    ArrayList类源码浅析(一)
    Long类源码浅析
    Integer类源码浅析
    JDK中String类的源码分析(二)
    JDK中String类的源码分析(一)
    Struts2漏洞修复总结
    [LeetCode]-011-Longest Common Prefix
  • 原文地址:https://www.cnblogs.com/leolovexx/p/6261921.html
Copyright © 2011-2022 走看看