Quantcast
Channel: SCN : All Content - SAPUI5 Developer Center
Viewing all articles
Browse latest Browse all 6178

SAPUI5 App: How to convert a date from DatePicker to week number and vis versa?

$
0
0

Hi sapui5 experts there,

 

I have a UI where the DatePicker is used to give an input date by user, and the returned string is converted to a  Date object, say:

 

Sun May 18 2014 00:00:00 GMT+0200 (Romance Daylight Time).

 

Now I want to use this Date object to extract a week number. I found the following code for extending Date class:

 

Date.prototype.getWeek = function() {

      var onejan = new Date(this.getFullYear(), 0, 1);

      return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);

}

 

However, I found this code has problems: 1) It returns a week number in range 2-53, but not 1-52. 2) For some year (maybe a leap year), eg, 2016, it returns week number which is shifted by 1.

 

Do any of you know some algorithm which is approved and reliable for returning the right week number based on a given date?

 

Furthermore, I also want to know how to get two days of a week based on a given week number. Any ideas?

 

Jsut for curiosity, these functionalities look like essential for the Date object, why aren't they be implemented as built-in methods for Date class?

 

Br,



Dong Zhu


Viewing all articles
Browse latest Browse all 6178

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>