Preface
Date and time in JavaScript
EXT JS 4 Implementation with week (week) Date selection control (combat one)
If you need to understand some of the preliminary knowledge of this article, you can refer to the above two articles.
Javascript has provided a Date object for processing time. But date does not provide a way to get the week.
To get a certain time on the web side by JS is the first week of the year and can be implemented according to some algorithms. Of course, jquery's extension components are available directly to provide such some ready-made packages.
Like Ext JS There is a way to provide access to the week Ext.Date.getWeekOfYear (Date).
problem
Whether the use of Ext JS can be a perfect solution to the problem of the week?
In the first article of this series, it is said that EXT DatePicker can not see the week, need to expand their own.
However, there is a problem with the extension:
The date object for the Javascript language starts every week from Sunday.
and Ext JS Getweekofyear This method is to follow ISO-8601, weekly is starting from Monday. (Other methods have not followed this standard, Ext JS mixed with different date time to express the standard).
Ext.Date.getWeekOfYear The return value of this method is the number between 1-53.
In this case, there will be some problems:
Ext JS Date Display control is displayed starting from Sunday: (s M t W t F s (Sunday Monday Tuesday ... Saturday))
However, the week was selected by the time it was chosen to start from Sunday. Lead:
1. Every Sunday week will be small 1 (for example 2013/08/18, Sunday, should be 34 weeks, but this method is counted as the end of the week, 33 weeks)
Copy Code code as follows:
Date = new Date ("2013/08/18");
var week = Ext.Date.getWeekOfYear (Date);
Alert ("week=" +week);
2. EXT Js Date control is displayed by default for 42 days, so that the problem will occur in the two-year interactive place.
Is it showing 53 weeks? It's the first week of the next year.
Solution
Combining the Date object of JS and the ext.date of Ext JS, the realization gets the week string.
1. Weekly Sunday as the first day
2. The number of weeks per year from (1-52), if more than 52 weeks, is counted to the first week of the following year. For example, 2013/12/29 is 2013 years of 53 weeks, the first week of 2014
3. Returns the format of a week such as "W1334"
Copy Code code as follows:
/*
* Return as W1334 () 2013/08/20
* 1. If sunday==> week = week+1
* Getweekofyear (EXT use Iso-8601,week begin Monday)
* JS Date (week begin Sunday)
* 2. If week > 52==> year = year +1; Week = week-52;
* 3. If month ==11 (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;
}