[Ext JS 4] Date Selection Control with Week (week) in practice (2)

Source: Internet
Author: User
Preface

Date and Time in Javascript

[Ext JS 4] Date Selection Control with Week (week) in practice (1)

For more information about the preparations in this article, see the above two articles.

Javascript provides date objects for processing time. However, date does not provide a method for obtaining the week.

You can use some algorithms to obtain the week number of the year from the Web Client in JS mode. Of course, jquery's extension components directly provide such ready-made packages.

For example, ext JS provides the method for obtaining the week Ext. Date. getweekofyear (date ).

Problem

If ext JS is used, can the week problem be solved perfectly?

As mentioned in the first article of this series, ext's datepicker cannot see the week and needs to be expanded on its own.

However, there is a problem during expansion:

The date object in Javascript language starts every week on Sunday.

While ext JS's getweekofyear method is followed by ISO-8601, every week from Monday. (Other methods do not follow this standard. Ext JS uses a mixture of different Date and Time Representation standards ).

The Return Value of the Ext. Date. getweekofyear method is the number between 1 and 53.

In this case, some problems will occur:

Ext js date display controls start from Sunday: (s m t w t f S (Sunday Monday Tuesday... Saturday ))

However, the selected time indicates that the week starts from Sunday. Cause:

1. The week of every Sunday will be smaller than 1 (for example, 2013/08/18, Sunday, it should be 34 weeks, but this method is counted as the end of the previous week, 33 weeks)

date = new Date("2013/08/18");var week = Ext.Date.getWeekOfYear(date);alert("week="+week);

2. The ext JS date control displays 42 days by default. In this way, problems may occur in the interaction between the two years.

Is it 53 weeks in that year? Or the first week of the next year.

Solution

Combined with the date object of JS and Ext. Date of ext JS, the week string is obtained.

1. Sunday is the first day of every week.

2. the number of weeks in each year ranges from 1 to 52. If the number of weeks exceeds 52, It is counted as the first week of the next year. For example, 2013 is the 53 week of, and the first week of is counted.

3. Return the week format such as "w1334 ".

/* * 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(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-6var weekday = date.getDay();if(weekday===0){dateWeek++;}// week>52 ==> year +1if(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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.