Some related operations based on the Data object in JavaScript

This article combines some usages on the Internet to encapsulate some functions to obtain various date usages, which can be used to make calendars, curriculums and other related functions


1. Get the current date (year month day)

var date = new Date();
//date
var curYear = date.getFullYear();
var curMonth = date.getMonth() + 1 ;     //The month on the system starts from 0, so add one
var curDate = date.getDate()

2. Get the number of days in this month (year and month) 

//Get the number of days in this month
getThisMonthDays(year, month) {
returnnew Date(year, month, 0).getDate();
},

//transfer
getThisMonthDays(2018,04)


3. Determine whether the current date is the week of the month  (year month day)

/ / Determine the current date is the week of the month 
getMonthWeek(year, month, data) {
//a = current date
 //w=current day of the week
//b = 6 - w = how many days are left in the current week (not counting today)
//The sum of a + b divided by 7 is the week of the current month
var date = new Date(),     
w = date.getDay(),    
d = date.getDate();
return Math.ceil((d + 6 - w) / 7 );    //The current date plus the time before this week is divided by 7 and rounded up to get the week of the month
},

//transfer
getMonthWeek(2018,04)

4. Get how many weeks  (year and month) the current month has

//Get how many weeks in the current month 
getWeeks(year, month) {
var d = new Date();
// first day of the month
d.setFullYear(year, month -1,1);
var w1 = d.getDay();
if (w1 == 0) w1 =7;
// days of the month
d.setFullYear(year, month,0);
var dd = d.getDate();
// first Monday
let d1;
if (w1 != 1) d1 =7 - w1 + 2;
else d1 = 1;
let week_count = Math.ceil((dd - d1 + 1) /7);
returnweek_count;
},

//调用
getWeeks(2018,04,01)

5.获取当前日期对应的周数数(’年月日‘)

//获取当前日期对应的周数数组
getWeek(target) {
let now = newDate(target);
let now_day = now.getDay();
let now_time = now.getTime();
let result = [0,1,2,3,4,5,6]
returnresult.map(i => (newDate(now_time +24 * 60* 60 *1000 * (i - now_day))).getDate())
},
//调用target里放得是日期字符串
getWeek('2018,04,01')


6.获取这个月第一天是周几(年月)

//获取这个月第一天是周几
getFirstDayOfWeek(year, month) {
returnnew Date(Date.UTC(year, month - 1, 1)).getDay();
},

7.把数字周几转化为汉字周记  0-周日

//把数字周几转化为汉字周记  0-周日
getDayName(day) {
        var day=parseInt(day);
        if(isNaN(day) || day<0 || day>6)
            return false;
        var weekday=["周日","周一","周二","周三","周四","周五","周六"];
        return weekday[day];
    }

8. 获取当前周  周一到周日的日期 (年月周)

//获取当前周  周一到周日的日期    如果是要周日到周六-1就行
getWeekTime(year, month,weekday) {
        var d = new Date();
        // 该月第一天
        d.setFullYear(year, month-1, 1);
        var w1 = d.getDay();
        if (w1 == 0) w1 = 7;
        // 该月天数
        d.setFullYear(year, month, 0);
        var dd = d.getDate();
        // 第一个周一
        let d1;
        if (w1 != 1) d1 = 7 - w1 + 2;
        else d1 = 1;
        var monday = d1+(weekday-1)*7;
        var sunday = monday + 6;
        var from = year+"-"+month+"-"+monday;
        var to;
        if (sunday <= dd) {
            to = year+"-"+month+"-"+sunday;
        } else {
            d.setFullYear(year, month-1, sunday);
            let days=d.getDate();
            to = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+days;
        }
        console.log(weekday+" 从" + from + " 到 " + to + "");
    }






EG:  下面是关于自己编程时在小程序上出现的一个小的bug,可能跟游览器支持的日期字符串不同的问题有关,解决方法就是改成能够支持的日期字符串。

(1) 获取当前日期所在周对应的days数组的函数

  //获取当前日期对应的周数数组     target:'2018,4,24'    '2018-4-24'    '2018/4/24'

getWeek(target) {      
letnow = new Date(target);
letnow_day = now.getDay();
letnow_time = now.getTime();
letresult = [0,1,2,3,4,5,6]
returnresult.map(i => (newDate(now_time +24 * 60* 60 *1000 * (i - now_day))).getDate())
},


(2) 后面调用

/ / 调用函数新日期对应的天数数组

iPhone上:
varnew_week_datacount = that.getWeek(newYear +'/' + newMonth + '/' + newData);

安卓~:
varnew_week_datacount = that.getWeek(newYear +',' + newMonth + ',' + newData);    
varnew_week_datacount = that.getWeek(newYear +'-' + newMonth + '-' + newData);   


tip:只在iPhone手机上做了测试 ,可以三种方式都尝试一下

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325843594&siteId=291194637