Ext.Date is a singleton that encapsulates a series of date operation functions and extends the functions of JavaScript Date. The following lists some commonly used functions.
Basic functions:
Let’s take a look at some examples in detail below:
// Ext.Date.add(date, interval, value) Add or decrease time to date. This function does not change the value of the original Date object, but returns a new Date object. // @param {Date} date Original date object. // @param {String} interval value unit, you can choose Ext.Date.DAY, Ext.Date.HOUR, Ext.Date.MINUTE, Ext.Date.MONTH, // Ext.Date.SECOND, Ext.Date.YEAR, Ext.Date.MILLI. // @param {number} value The value that the date object needs to be added. // @return {Date} Returns the Date object after the added value. // Example var date = Ext.Date.add(new Date('10/29/2006'), Ext.Date.DAY, 5); //Add 5 days console.log(date); //Return result Fri Nov 03 2006 00:00:00 GMT+0800 (China Standard Time) var date = Ext.Date.add(new Date('10/29/2006'), Ext.Date.DAY, -5); //Reduce 5 days, if the value is negative, it will decrease. console.log(date); //Return result Tue Oct 24 2006 00:00:00 GMT+0800 (China Standard Time) var date = Ext.Date.add(new Date('10/29/2006'), Ext.Date.YEAR, 2); //Add 2 years console.log(date); //Return result Wed Oct 29 2008 00:00:00 GMT+0800 (China Standard Time) // Ext.Date.between(date, start, end) Determine whether date is between start and end. // @param {Date} date The date to be judged. // @param {Date} start // @param {Date} end // @return {Boolean} If date returns true between start and end, otherwise return false. // Example var date = new Date('10/29/2006'); var start = new Date('10/5/2006'); var end = new Date('11/15/2006'); Ext.Date.between(date, start, end); // Return true // Ext.Date.clearTime(date, clone) Set the time of date to 00 hours, 00 minutes, 00 seconds, 000 milliseconds. // @param {Date} date // @param {Bollean} clone Optional parameters. If true, a copy of date is returned, and if false, the date itself is returned, which defaults to false. // @return {Date} Returns the set date. // Example var date = new Date('10/30/2012 14:30:00'); Ext.Date.clearTime(date); // Return Tue Oct 30 2012 00:00:00 GMT+0800 (China Standard Time) // Ext.Date.clone(date) A copy of date cloned. // @param {Date} date // @return {Date} Returns the cloned Date. // Example var orig = new Date('10/30/2012'); var copy = Ext.Date.clone(orig); //Clone a value // Ext.Date.format(date, format) Format the date and return the formatted string. // @param {Date} date date. // @param {String} format Date format, Y-year, m-month, d-day, H-24 hours, i-minute, s-seconds// @return {String} Returns the formatted string. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.format(date, 'Ymd H:i:s'); // 2012-10-20 14:30:00 Ext.Date.format(date, 'Y year m month d date H:i:s'); // October 20, 2012 14:30:00 // Ext.Date.getDayOfYear(date) Get the date of the year // @param {Date} date date. // @return {Number} Returns the number of days, with a value range of 0~364, and if it is a leap year, there is 365. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.getDayOfYear(date); //Return 293 // Ext.Date.getDaysInMonth(date) Get the date is the day of the month // @param {Date} date date. // @return {Number} Returns the number of days. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.getDayOfYear(date); // Return 31 // Ext.Date.getFirstDateOfMonth(date) Get the first day of the month where the date is located // @param {Date} date date. // @return {Date} Returns the first day of the month. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.getFirstDateOfMonth(date); // Return Mon Oct 01 2012 00:00:00 GMT+0800 (China Standard Time) // Ext.Date.getFirstDayOfMonth(date) Get the week of the first day of the month where the date is located // @param {Date} date date. // @return {Number} Returns the week of the first day of the month, with a value range of 0~6. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.getFirstDayOfMonth(date); // Return 1, indicating Monday // Ext.Date.getLastDateOfMonth(date) Get the last day of the month where the date is located // @param {Date} date date. // @return {Date} Returns the last day of the month you are in. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.getLastDateOfMonth(date); // Return Wed Oct 31 2012 00:00:00 GMT+0800 (China Standard Time) // Ext.Date.getLastDayOfMonth(date) Get the week of the last day of the month where the date is located // @param {Date} date date. // @return {Number} Returns the week of the last day of the month, with a value range of 0~6. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.getLastDayOfMonth(date); // Return 3, indicating Wednesday // Ext.Date.getWeekOfYear(date) Get the week of the year in which date is located // @param {Date} date date. // @return {Number} Returns the week of the year, with a value range of 1~53. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.getWeekOfYear(date); // Return 42 // Ext.Date.isLeapYear(date) Is the year where the date is located? // @param {Date} date Date. // @return {Boolean} true means leap year, false means flat year. // Example var date = new Date('10/20/2012 14:30:00'); Ext.Date.isLeapYear(date); //Return true // Ext.Date.now() Returns the number of milliseconds from the current time to January 1, 1970. // Date.now() already implements the same function in chrome, ie9 and ie10. // @return {Number} Returns the number of milliseconds. // Example var timestamp = Ext.Date.now(); //1351666679575 var date = new Date(timestamp); //Wed Oct 31 2012 14:57:59 GMT+0800 (China Standard Time) // Ext.Date.parse(input, format, strict) Date.parse() has similar functions according to the input string creation date. // @param {String} input Date string. // @param {String} format date format. // @param {Boolean} strict Verify the validity of the input string, which is false by default. // @param {Date} Returns the date of creation. // Example var input = 'October 31, 2012 14:30:00'; var format = 'Y year m month d day H:i:s'; var date = Ext.Date.parse(input, format, true); //Wed Oct 31 2012 14:30:00 GMT+0800 (China Standard Time)Example: Implement date selection control with week (week)
1. Question:
Can you perfectly solve the week's problem by using Ext JS?
In the first article of this series, it is said that Ext's datepicker cannot be seen for the week and needs to be expanded by itself.
However, there is a problem when expanding:
The Javascript language Date objects start on Sundays each week.
However, Ext JS's getWeekOfYear method follows ISO-8601, and starts every week from Monday. (There are other methods that do not follow this standard, Ext JS mixes different date and time representation standards).
Ext.Date.getWeekOfYear The return value of this method is a number between 1 and 53.
In this way, some problems will arise:
The display of the date display control of Ext js starts on Sunday: (SMTWTFS (Sunday Monday Tuesday.. Saturday))
However, when you get the week by the selected time, it starts from Sunday. lead to:
Every Sunday will be 1 less (for example, on Sunday, 2013/08/18, it should be 34 weeks, but this method is calculated as the end of the previous week, 33 weeks)
date = new Date("2013/08/18");
var week = Ext.Date.getWeekOfYear(date);
alert("week="+week);
The Ext Js date control is displayed for 42 days by default, so there will be problems in the interaction between two years.
Is it 53 weeks that year? It's still the first week of the next year.
2. Solution:
Combining the Date object of js and the Ext.Date of Ext js, we realize the acquisition of the week string.
Sunday is the first day of each week
The number of weeks per year is from (1-52). If it exceeds 52 weeks, it will be counted to the first week of the next year. For example, 2013/12/29 is the 53 week of 2013, which is counted to the first week of 2014.
Return to the week format like "W1334"
/* * return as W1334()2013/08/20 * 1. if sunday==> week = week+1 * getWeekOfYear(Ext use ISO-8601,week begin month) * js Date(week begin sunday) * 2. if week > 52==> year = year +1; week = week - 52; * 3. if month ==11(12 month) and week <2 ==> year = year +1; */function getWeekStrOfDate(date){ var weekStr = null; if(date!=null) { weekStr = "W"; var dateYear = date.getFullYear(); var dateWeek = Ext.Date.getWeekOfYear(date); var firstDayOfMonth = Ext.Date.getFirstDayOfMonth(date); var day = date.getDate(); var month = date.getMonth(); //weekday 0-6 var weekday = date.getDay(); if(weekday===0) { dateWeek++; } // week>52 ==> year +1 if(month==11) { if(dateWeek>52) { dateYear += 1; dateWeek -= 52; }else if(dateWeek<2){ dateYear += 1; } } var yearStr = dateYear.toString(); yearStr = yearStr.substring(2,4); var dateWeekStr = dateWeek.toString(); if(dateWeekStr.length<2) { dateWeekStr = "0" + dateWeekStr; } weekStr += yearStr; weekStr += dateWeekStr; } return weekStr;}