Different dates being returned depending on region - javascript

I have a function in Javascript that returns the date ranges of the current/previous/next quarter. For example, for current quarter it would return 2019-01-01 and 2019-03-31. For some reason, a few colleagues have said that the date ranges are inaccurate for them: for them it returns 2018-12-31 and 2019-02-27. I noticed that both of these users are in Germany/Poland region.
Here is my jsFiddle
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getUTCMonth() + 1),
day = '' + d.getUTCDate(),
year = d.getUTCFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}
function getDate_FQ(range){
var now = new Date();
switch(range){
case 'Previous FQ':
now.setMonth(now.getMonth()-3);
break;
case 'Next FQ':
now.setMonth(now.getMonth()+3);
break;
case 'Current FQ':
break;
}
var quarter = Math.floor((now.getUTCMonth() / 3));
var firstDate = new Date(now.getUTCFullYear(), quarter * 3, 1);
var endDate = new Date(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0);
return([firstDate, endDate])
}
let [first, end] = getDate_FQ('Current FQ')
console.log(formatDate(first), formatDate(end))
How is it that one date is off by 1 day and the other is off by 1 month and 1 day?

If you want to have Date refers to the same time in every timezone, work in UTC and change your code in two lines to:
var firstDate = new Date(Date.UTC(now.getUTCFullYear(), quarter * 3, 1));
var endDate = new Date(Date.UTC(firstDate.getUTCFullYear(), firstDate.getUTCMonth() + 3, 0));

NaDeR Star posted a solution that solved my problem with the timezone but I found a method that was nearly twice as fast when running this 100K times.
Since the dates of the quarter ranges are always the same (Jan1 - Mar31, Apr1 - Jun30, etc) and the only thing changing is the year, it is faster to just combine the year with the date range.
Here is my new solution:
function getDate_FQ(range){
var now = new Date();
var dateArr = {0: ['-01-01', '-03-31'], 1: ['-04-01', '-06-30'], 2: ['-07-01', '-09-30'], 3: ['-10-01', '-12-31']}
switch(range){
case 'Previous FQ':
now.setUTCMonth(now.getUTCMonth()-3);
break;
case 'Next FQ':
now.setUTCMonth(now.getUTCMonth()+3);
break;
case 'Current FQ':
break;
}
var quarter = Math.floor((now.getUTCMonth() / 3));
var dates = dateArr[quarter]
var firstDate = (now.getUTCFullYear()) + dates[0];
var endDate = (now.getUTCFullYear()) + dates[1];
return([firstDate, endDate])
}

Related

Manipulating Date/time with JS and moment

I have 2 dates and I need to create a 3rd date using those 2 dates.
if (document.getElementById("endDate"))
endDate = document.getElementById("endDate").value;
if (document.getElementById("presentDate"))
presentDate = document.getElementById("presentDate").value;
If Present date = "12/5/2018" and End date = "12/25/2018" then my New date = "12/26/2018";
Since, JavaScript date months range from 0-11 and also the dates are kind of messy, I am not getting the desired results.
What I tried:
var presentDt = new Date(presentDate);
var endDt = new Date(endDate);
var newDay = endDt.getUTCDate()+1;
var presentMonth = presentDt.getUTCMonth();
var presentYear = presentDt.getUTCFullYear();
var nextDate= presentMonth + '/' + endDay + '/' + presentYear;
Issue 1: This above code works but if my endDate is on the 31st, then adding UTCDate+1 makes it 32, which returns invalid date.
Issue 2: If I do UTCMonth(), it returns 11 but if I want to add 2 months then it returns 13 which is also invalid. The issue is basically that I am not able to manipulate the dates as I want.
I also tried moment, however I am having similar issues and I am not able to manipulate the dates easily as I want them to.
Also tried getDate and getMonth but it does the same thing.
Is there a better way of handling the overall date/time with JavaScript?
1: add/subtract days:
new Date(new Date().setDate(new Date().getDay() + 3))
2: add/subtract months:
new Date(new Date().setMonth(new Date().getMonth() + 3))
//your code
var presentDt = new Date(presentDate);
var endDt = new Date(endDate);
var newDay = new Date(new Date().setDate(endDt.getDay() + 2));
var presentMonth = presentDt.getUTCMonth();
var presentYear = presentDt.getUTCFullYear();
Since your input string is not in a format recognized by new Date() across environments, I suggest to use moment(String, String) to parse it.
Then you can use moment(Object), moment's setters (like year(), month() and date()) and format() to get the desired output.
Here a live sample:
var presentDt = "12/5/2018";
var endDt = "12/25/2018";
// Parse your input with momentjs
var mPresent = moment(presentDt, "MM/DD/YYYY");
var mEnd = moment(endDt, "MM/DD/YYYY");
// Create a new momnt object compliant with your needs
var nextDate = moment({
year: mPresent.year(), // get presentDt's year
month: mPresent.month(), // get presentDt's month
date: mEnd.add(1, 'day').date() // get endDt day of the month and add 1 day to it
});
// Display the result in the format you need
console.log(nextDate.format("MM/DD/YYYY"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Note that you will get 01 as day result when endDt represents the last day of the month.
This is what i did:
First i created the following functions to look at the number of days and months.
function formattedDate(date) {
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return (monthIndex + 1) + '/' + day + '/' + year;
}
function myNewDate(datestring) {
var dates = datestring.split("/");
var year, month, day;
if (dates.length < 3) {
dates = datestring.split("-");
year = dates[0];
month = dates[1] - 1;
day = dates[2];
} else {
year = dates[2];
month = dates[0] - 1;
day = dates[1];
}
var days = [31, 28, 31, 30, 31, 30 ,31, 31, 30, 31, 30, 31];
// Overflow day
if (day > days[month])
day = days[month];
return new Date(year, month, day);
}
function newDate(date1, date2) {
var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var year = date1.getFullYear();
var month = date1.getMonth() + 1;
var day = date2.getDate() + 1;
// Check leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
days[1] = 29;
// Overflow day
if (day > days[month])
day = days[month];
var newdate = new Date(year, month, day);
return newdate;
}
Here, i used the above function to create my new date. You can also add/subtract days/months as you want by updating the above.
var mpresent = myNewDate(presentDate);
var mstart = myNewDate(startDate);
var myDate = (formattedDate(newDate(mpresent, mstart)));

Delivery Date Javascript Adapt To Exclude Weekends

I have been currently using this script
<script type="text/javascript">
var _next = new Date(new Date() * 1 + 24*60*60*1000*9);
document.write(" Your expected delivery date is " + (_next.getMonth() + 1) + "/" + _next.getDate() + "/" + _next.getFullYear());
</script>
I'm wondering how do I get it to exclude weekends if possible.
Just add hours if you like. this is a code that expects to deliver the next working day.
function myFunction() {
var d = new Date();
var weekday = new Array(7);
weekday[0] = "sunday";
weekday[1] = "monday";
weekday[2] = "tuesday";
weekday[3] = "wednesday";
weekday[4] = "thursday";
weekday[5] = "friday";
weekday[6] = "saturday";
var monthday = new Array(12);
monthday[0] = "01";
monthday[1] = "02";
monthday[2] = "03";
monthday[3] = "04";
monthday[4] = "05";
monthday[5] = "06";
monthday[6] = "07";
monthday[7] = "08";
monthday[8] = "09";
monthday[9] = "10";
monthday[10] = "11";
monthday[11] = "12";
if (d.getDay() > 0 && d.getDay() <= 4) {
d.setHours(d.getHours() + 24);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
var year = d .getFullYear();
document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
} else if (d.getDay() == 0) {
d.setHours(d.getHours() + 48);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
var year = d .getFullYear();
document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
} else {
d.setHours(d.getHours() + 72);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
var year = d .getFullYear();
document.getElementById("deliveryday").innerHTML = deliver + ' ' + day + '-' + month + '-' + year;
}
}
myFunction()
<script type="text/javascript">
<!--
var myDate=new Date();
if ( myDate.getHours() < 12 ) // less than 12pm
{
var daystodeliver = [7,5,5,5,5,8,7][myDate.getDay()];
}
else
{
var daystodeliver = [7,7,7,7,7,9,8][myDate.getDay()];
}
myDate.setDate(myDate.getDate()+daystodeliver);
document.write(['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'] [myDate.getDay()]);
var dayofmonth = myDate.getDate();
suffix = ((dayofmonth < 10)||(dayofmonth > 20)) ? ['th','st','nd','rd','th','th','th','th','th','th'][dayofmonth % 10] : 'th';
document.write(' ' + dayofmonth + suffix + ' ');
document.write(['January','February','March','April','May','June','July','August','Septembe r','October','November','December'][myDate.getMonth()]);
// -->
Try this:
function deliver(inDays, startingOn){
var s, f = 0, d;
if(!inDays)inDays = 0;
s = !startingOn ? new Date : new Date(startingOn);
for(var i=0,n,t=0,l=inDays; i<l; i++,t+=86400000){
n = new Date(s.getTime()+t).getDay();
if(n === 0 || n === 6)f++;
}
d = new Date(s.getTime()+86400000*(inDays+f));
return 'Your expected delivery date is '+d.toLocaleDateString();
}
// same day delivery
console.log(deliver());
// deliver in 9 days stating today
console.log(deliver(9));
// deliver in 9 days starting on October 12, 2013 - must be a valid Date String
console.log(deliver(9, 'October 12, 2013'));
/* Note that in the last example the Date starts on the weekend, therefore same
day becomes Monday, which if you don't work weekends is the first day you
would see the order anyways.
*/
This is good to show your Client when something is delivered. Make sure you set the actual date it your database, according to where you are, using a Server Side language like PHP.
You should pay me for this since it will work even on a leap year. Ha!
Try this to get the date
I wrote a routine that you supply a date object, apply the menthod, the public holidays in a separate list is passed and any holidays / public holidays including weekends are skipped over.
Its this post
// array of ISO YYYY-MM-DD format dates
publicHolidays = {
uk:["2020-01-01","2020-04-10","2020-04-13","2020-05-08","2020-05-25",
"2020-08-03","2020-08-31","2020-12-25","2020-12-28"],
usa:["2020-01-01","2020-01-20","2020-02-14","2020-02-17","2020-04-10",
"2020-04-12","2020-05-10","2020-05-25","2020-06-21","2020-07-03",
"2020-07-04","2020-09-07","2020-10-12","2020-10-31","2020,11,11",
"2020-11-26","2020-12-25"]
}
// check if there is a match in the array
Date.prototype.isPublicHoliday = function( data ){// we check for a public holiday
if(!data) return 1;
return data.indexOf(this.toISOString().slice(0,10))>-1? 0:1;
}
// calculation of business days
Date.prototype.businessDays = function( d, holidays ){
var holidays = holidays || false, t = new Date( this ); // copy date.
while( d ){ // we loop while d is not zero...
t.setDate( t.getDate() + 1 ); // set a date and test it
switch( t.getDay() ){ // switch is used to allow easier addition of other days of the week
case 0: case 6: break;// sunday & saturday
default: // check if we are a public holiday or not
d -= t.isPublicHoliday( holidays );
}
}
return t.toISOString().slice(0,10); // just the YYY-MM-DD
}
// dummy var, could be a form field input
OrderDate = "2020-02-12";
// test with a UK holiday date
var deliveryDate = new Date(OrderDate).businessDays(7, publicHolidays.usa);
// expected output 2020-02-25
console.log("Order date: %s, Delivery date: %s",OrderDate,deliveryDate );

Javascript: get Monday and Sunday of the previous week

I am using the following script to get Monday (first) and Sunday (last) for the previous week:
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay() - 6; // Gets day of the month (e.g. 21) - the day of the week (e.g. wednesday = 3) = Sunday (18th) - 6
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));
This works fine if last Monday and Sunday were also in the same month, but I just noticed today that it doesn't work if today is December and last Monday was in November.
I'm a total JS novice, is there another way to get these dates?
You can get the previous Monday by getting the Monday of this week and subtracting 7 days. The Sunday will be one day before that, so:
var d = new Date();
// set to Monday of this week
d.setDate(d.getDate() - (d.getDay() + 6) % 7);
// set to previous Monday
d.setDate(d.getDate() - 7);
// create new date of day before
var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() - 1);
For 2012-12-03 I get:
Mon 26 Nov 2012
Sun 25 Nov 2012
Is that what you want?
// Or new date for the following Sunday
var sunday = new Date(d.getFullYear(), d.getMonth(), d.getDate() + 6);
which gives
Sun 02 Dec 2012
In general, you can manipulate date objects by add and subtracting years, months and days. The object will handle negative values automatically, e.g.
var d = new Date(2012,11,0)
Will create a date for 2012-11-30 (noting that months are zero based so 11 is December). Also:
d.setMonth(d.getMonth() - 1); // 2012-10-30
d.setDate(d.getDate() - 30); // 2012-09-30
if you dont want to do it with an external library you should work with timestamps. i created a solution where you would substract 60*60*24*7*1000 (which is 604800000, which is 1 week in milliseconds) from the current Date and go from there:
var beforeOneWeek = new Date(new Date().getTime() - 60 * 60 * 24 * 7 * 1000)
, day = beforeOneWeek.getDay()
, diffToMonday = beforeOneWeek.getDate() - day + (day === 0 ? -6 : 1)
, lastMonday = new Date(beforeOneWeek.setDate(diffToMonday))
, lastSunday = new Date(beforeOneWeek.setDate(diffToMonday + 6));
You could use a library like moment.js.
See the subtract method http://momentjs.com/docs/#/manipulating/subtract/
A few answers mentioned moment, but no one wrote about this simple method:
moment().day(-13) // Monday last week
moment().day(-7) // Sunday last week
.day sets a week day, so it doesn't matter what day is it today, only week matters.
This is the general solution of find any day of any week.
function getParticularDayTimestamp(lastWeekDay) {
var currentWeekMonday = new Date().getDate() - new Date().getDay() + 1;
return new Date().setDate(currentWeekMonday - lastWeekDay);
}
console.log(getParticularDayTimestamp(7)) // for last week monday
console.log(getParticularDayTimestamp(1)) // for last week sunday
console.log(getParticularDayTimestamp(14)) // for last to last week monday
console.log(getParticularDayTimestamp(8)) // for last to last week sunday
Using Moment you can do the following
var lastWeek = moment().isoWeek(moment().subtract(1,'w').week());
var mondayDifference = lastWeek.dayOfYear() - lastWeek.weekday() + 1;
var sundayDifference = mondayDifference - 1;
var lastMonday = moment().dayOfYear(mondayDifference);
var lastSunday = moment().dayOfYear(sundayDifference );
it can be this simple.
var today = new Date();
var sunday = new Date(this.today.getFullYear(), this.today.getMonth(), this.today.getDate() - this.today.getDay());
Here you have a multi-purpose function:
function getThe(numOfWeeks, weekday, tense, fromDate) {
// for instance: var lastMonday = getThe(1,"Monday","before",new Date())
var targetWeekday = -1;
var dateAdjustment = clone(fromDate);
var result = clone(fromDate);
switch (weekday) {
case "Monday": targetWeekday = 8; break;
case "Tuesday": targetWeekday = 2; break;
case "Wednesday": targetWeekday = 3; break;
case "Thursday": targetWeekday = 4; break;
case "Friday": targetWeekday = 5; break;
case "Saturday": targetWeekday = 6; break;
case "Sunday": targetWeekday = 7;
}
var adjustment = 7 * (numOfWeeks - 1);
if (tense == "after") adjustment = -7 * numOfWeeks;
dateAdjustment.setDate(fromDate.getDate() - targetWeekday);
var weekday = dateAdjustment.getDay();
result.setDate(fromDate.getDate() - weekday - adjustment);
result.setHours(0,0,0,0);
return result;
}
You can find the "clone(obj)" function in the next post: https://stackoverflow.com/a/728694/6751764
You can use a third party date library to deal with dates. For example:
var startOfWeek = moment().startOf('week').toDate();
var endOfWeek = moment().endOf('week').toDate();
if you want to use JavaScript then use the below code
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay()+1; // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var startDate = new Date(curr.setDate(first));
startDate = ""+startDate.getFullYear()+"-"+ (startDate.getMonth() + 1) + "-" + startDate.getDate()
var endDate = new Date(curr.setDate(last));
endDate = "" + (endDate.getMonth() + 1) + "/" + endDate.getDate() + "/" + endDate.getFullYear();
alert(startDate+" , "+endDate)

Show week number with Javascript?

I have the following code that is used to show the name of the current day, followed by a set phrase.
<script type="text/javascript">
<!--
// Array of day names
var dayNames = new Array(
"It's Sunday, the weekend is nearly over",
"Yay! Another Monday",
"Hello Tuesday, at least you're not Monday",
"It's Wednesday. Halfway through the week already",
"It's Thursday.",
"It's Friday - Hurray for the weekend",
"Saturday Night Fever");
var now = new Date();
document.write(dayNames[now.getDay()] + ".");
// -->
</script>
What I would like to do is have the current week number in brackets after the phrase. I have found the following code:
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
Which was taken from http://javascript.about.com/library/blweekyear.htm but I have no idea how to add it to existing javascript code.
Simply add it to your current code, then call (new Date()).getWeek()
<script>
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
}
var weekNumber = (new Date()).getWeek();
var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var now = new Date();
document.write(dayNames[now.getDay()] + " (" + weekNumber + ").");
</script>
In case you already use jQuery-UI (specifically datepicker):
Date.prototype.getWeek = function () { return $.datepicker.iso8601Week(this); }
Usage:
var myDate = new Date();
myDate.getWeek();
More here: UI/Datepicker/iso8601Week
I realize this isn't a general solution as it incurs a dependency. However, considering the popularity of jQuery-UI this might just be a simple fit for someone - as it was for me.
If you don't use jQuery-UI and have no intention of adding the dependency. You could just copy their iso8601Week() implementation since it is written in pure JavaScript without complex dependencies:
// Determine the week of the year (local timezone) based on the ISO 8601 definition.
Date.prototype.iso8601Week = function () {
// Create a copy of the current date, we don't want to mutate the original
const date = new Date(this.getTime());
// Find Thursday of this week starting on Monday
date.setDate(date.getDate() + 4 - (date.getDay() || 7));
const thursday = date.getTime();
// Find January 1st
date.setMonth(0); // January
date.setDate(1); // 1st
const jan1st = date.getTime();
// Round the amount of days to compensate for daylight saving time
const days = Math.round((thursday - jan1st) / 86400000); // 1 day = 86400000 ms
return Math.floor(days / 7) + 1;
};
console.log(new Date().iso8601Week());
console.log(new Date("2020-01-01T00:00").iso8601Week());
console.log(new Date("2021-01-01T00:00").iso8601Week());
console.log(new Date("2022-01-01T00:00").iso8601Week());
console.log(new Date("2023-12-31T00:00").iso8601Week());
console.log(new Date("2024-12-31T00:00").iso8601Week());
Consider using my implementation of "Date.prototype.getWeek", think is more accurate than the others i have seen here :)
Date.prototype.getWeek = function(){
// We have to compare against the first monday of the year not the 01/01
// 60*60*24*1000 = 86400000
// 'onejan_next_monday_time' reffers to the miliseconds of the next monday after 01/01
var day_miliseconds = 86400000,
onejan = new Date(this.getFullYear(),0,1,0,0,0),
onejan_day = (onejan.getDay()==0) ? 7 : onejan.getDay(),
days_for_next_monday = (8-onejan_day),
onejan_next_monday_time = onejan.getTime() + (days_for_next_monday * day_miliseconds),
// If one jan is not a monday, get the first monday of the year
first_monday_year_time = (onejan_day>1) ? onejan_next_monday_time : onejan.getTime(),
this_date = new Date(this.getFullYear(), this.getMonth(),this.getDate(),0,0,0),// This at 00:00:00
this_time = this_date.getTime(),
days_from_first_monday = Math.round(((this_time - first_monday_year_time) / day_miliseconds));
var first_monday_year = new Date(first_monday_year_time);
// We add 1 to "days_from_first_monday" because if "days_from_first_monday" is *7,
// then 7/7 = 1, and as we are 7 days from first monday,
// we should be in week number 2 instead of week number 1 (7/7=1)
// We consider week number as 52 when "days_from_first_monday" is lower than 0,
// that means the actual week started before the first monday so that means we are on the firsts
// days of the year (ex: we are on Friday 01/01, then "days_from_first_monday"=-3,
// so friday 01/01 is part of week number 52 from past year)
// "days_from_first_monday<=364" because (364+1)/7 == 52, if we are on day 365, then (365+1)/7 >= 52 (Math.ceil(366/7)=53) and thats wrong
return (days_from_first_monday>=0 && days_from_first_monday<364) ? Math.ceil((days_from_first_monday+1)/7) : 52;
}
You can check my public repo here https://bitbucket.org/agustinhaller/date.getweek (Tests included)
If you want something that works and is future-proof, use a library like MomentJS.
moment(date).week();
moment(date).isoWeek()
http://momentjs.com/docs/#/get-set/week/
It looks like this function I found at weeknumber.net is pretty accurate and easy to use.
// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: http://weeknumber.net/how-to/javascript
// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}
If you're lucky like me and need to find the week number of the month a little adjust will do it:
// Returns the week in the month of the date.
Date.prototype.getWeekOfMonth = function() {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), date.getMonth(), 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}
If you already use Angular, then you could profit $filter('date').
For example:
var myDate = new Date();
var myWeek = $filter('date')(myDate, 'ww');
By adding the snippet you extend the Date object.
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}
If you want to use this in multiple pages you can add this to a seperate js file which must be loaded first before your other scripts executes. With other scripts I mean the scripts which uses the getWeek() method.
All the proposed approaches may give wrong results because they don’t take into account summer/winter time changes. Rather than calculating the number of days between two dates using the constant of 86’400’000 milliseconds, it is better to use an approach like the following one:
getDaysDiff = function (dateObject0, dateObject1) {
if (dateObject0 >= dateObject1) return 0;
var d = new Date(dateObject0.getTime());
var nd = 0;
while (d <= dateObject1) {
d.setDate(d.getDate() + 1);
nd++;
}
return nd-1;
};
I was coding in the dark (a challenge) and couldn't lookup, bring in any dependencies or test my code.
I forgot what round up was called (Math.celi) So I wanted to be extra sure i got it right and came up with this code instead.
var elm = document.createElement('input')
elm.type = 'week'
elm.valueAsDate = new Date()
var week = elm.value.split('W').pop()
console.log(week)
Just a proof of concept of how you can get the week in any other way
But still i recommend any other solution that isn't required by the DOM.
With that code you can simply;
document.write(dayNames[now.getDay()] + " (" + now.getWeek() + ").");
(You will need to paste the getWeek function above your current script)
You could find this fiddle useful. Just finished.
https://jsfiddle.net/dnviti/ogpt920w/
Code below also:
/**
* Get the ISO week date week number
*/
Date.prototype.getWeek = function () {
// Create a copy of this date object
var target = new Date(this.valueOf());
// ISO week date weeks start on monday
// so correct the day number
var dayNr = (this.getDay() + 6) % 7;
// ISO 8601 states that week 1 is the week
// with the first thursday of that year.
// Set the target date to the thursday in the target week
target.setDate(target.getDate() - dayNr + 3);
// Store the millisecond value of the target date
var firstThursday = target.valueOf();
// Set the target to the first thursday of the year
// First set the target to january first
target.setMonth(0, 1);
// Not a thursday? Correct the date to the next thursday
if (target.getDay() != 4) {
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}
// The weeknumber is the number of weeks between the
// first thursday of the year and the thursday in the target week
return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000
}
/**
* Get the ISO week date year number
*/
Date.prototype.getWeekYear = function ()
{
// Create a new date object for the thursday of this week
var target = new Date(this.valueOf());
target.setDate(target.getDate() - ((this.getDay() + 6) % 7) + 3);
return target.getFullYear();
}
/**
* Convert ISO week number and year into date (first day of week)
*/
var getDateFromISOWeek = function(w, y) {
var simple = new Date(y, 0, 1 + (w - 1) * 7);
var dow = simple.getDay();
var ISOweekStart = simple;
if (dow <= 4)
ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
else
ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
return ISOweekStart;
}
var printDate = function(){
/*var dateString = document.getElementById("date").value;
var dateArray = dateString.split("/");*/ // use this if you have year-week in the same field
var dateInput = document.getElementById("date").value;
if (dateInput == ""){
var date = new Date(); // get today date object
}
else{
var date = new Date(dateInput); // get date from field
}
var day = ("0" + date.getDate()).slice(-2); // get today day
var month = ("0" + (date.getMonth() + 1)).slice(-2); // get today month
var fullDate = date.getFullYear()+"-"+(month)+"-"+(day) ; // get full date
var year = date.getFullYear();
var week = ("0" + (date.getWeek())).slice(-2);
var locale= "it-it";
document.getElementById("date").value = fullDate; // set input field
document.getElementById("year").value = year;
document.getElementById("week").value = week; // this prototype has been written above
var fromISODate = getDateFromISOWeek(week, year);
var fromISODay = ("0" + fromISODate.getDate()).slice(-2);
var fromISOMonth = ("0" + (fromISODate.getMonth() + 1)).slice(-2);
var fromISOYear = date.getFullYear();
// Use long to return month like "December" or short for "Dec"
//var monthComplete = fullDate.toLocaleString(locale, { month: "long" });
var formattedDate = fromISODay + "-" + fromISOMonth + "-" + fromISOYear;
var element = document.getElementById("fullDate");
element.value = formattedDate;
}
printDate();
document.getElementById("convertToDate").addEventListener("click", printDate);
*{
font-family: consolas
}
<label for="date">Date</label>
<input type="date" name="date" id="date" style="width:130px;text-align:center" value="" />
<br /><br />
<label for="year">Year</label>
<input type="year" name="year" id="year" style="width:40px;text-align:center" value="" />
-
<label for="week">Week</label>
<input type="text" id="week" style="width:25px;text-align:center" value="" />
<br /><br />
<label for="fullDate">Full Date</label>
<input type="text" id="fullDate" name="fullDate" style="width:80px;text-align:center" value="" />
<br /><br />
<button id="convertToDate">
Convert Date
</button>
It's pure JS.
There are a bunch of date functions inside that allow you to convert date into week number and viceversa :)
Luxon is an other alternative. Luxon date objects have a weekNumber property:
let week = luxon.DateTime.fromString("2022-04-01", "yyyy-MM-dd").weekNumber;
console.log(week);
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/3.0.1/luxon.min.js"></script>
I've tried using code from all of the answers above, and all return week #52 for the first of January. So I decided to write my own, which calculates the week number correctly.
Week numeration starts from 0
Maybe it's a bad taste to use a loop, or the result can be cached somewhere to prevent repeating the same calculations if the function is called often enough. Well, I have made this for myself, and it does what I need it to do.
Date.prototype.getWeek = function() {
// debugger
let msWeek = 604800000; // Week in milliseconds
let msDay = 86400000; // Day in milliseconds
let year = this.getFullYear(); // Get the year
//let month = this.getMonth(); // Month
let oneDate = new Date(year, 0, 1); // Create a new date based on THIS year
let temp = oneDate.getDay(); // Ordinal of the first day
let getFirstDay = (temp === 0) ? 6 : temp - 1; // Ordinal of the first day of the current month (0-MO, 6-SU)
let countWeek = 0;
// Test to confirm week
oneDate = new Date(oneDate.getTime() + msDay*(7 - getFirstDay));
if(oneDate.getTime() > this.getTime()){
return countWeek;
}
// Increment loop
while(true){
oneDate = new Date(oneDate.getTime() + msWeek); // Add a week and check
if(oneDate.getTime() > this.getTime()) break;
countWeek++;
}
return countWeek + 1;
}
let s1 = new Date('2022-01-01'); console.log(s1.getWeek());
let s2 = new Date('2023-01-01'); console.log(s2.getWeek());
let s22 = new Date('2023-01-02'); console.log(s22.getWeek());
let s3 = new Date('2024-01-01'); console.log(s3.getWeek());
let s4 = new Date('2025-01-01'); console.log(s4.getWeek());
let s5 = new Date('2022-02-28'); console.log(s5.getWeek());
let s6 = new Date('2022-12-31'); console.log(s6.getWeek());
let s7 = new Date('2024-12-31'); console.log(s7.getWeek());
Some of the code I see in here fails with years like 2016, in which week 53 jumps to week 2.
Here is a revised and working version:
Date.prototype.getWeek = function() {
// Create a copy of this date object
var target = new Date(this.valueOf());
// ISO week date weeks start on monday, so correct the day number
var dayNr = (this.getDay() + 6) % 7;
// Set the target to the thursday of this week so the
// target date is in the right year
target.setDate(target.getDate() - dayNr + 3);
// ISO 8601 states that week 1 is the week with january 4th in it
var jan4 = new Date(target.getFullYear(), 0, 4);
// Number of days between target date and january 4th
var dayDiff = (target - jan4) / 86400000;
if(new Date(target.getFullYear(), 0, 1).getDay() < 5) {
// Calculate week number: Week 1 (january 4th) plus the
// number of weeks between target date and january 4th
return 1 + Math.ceil(dayDiff / 7);
}
else { // jan 4th is on the next week (so next week is week 1)
return Math.ceil(dayDiff / 7);
}
};
Martin Schillinger's version seems to be the strictly correct one.
Since I knew I only needed it to work correctly on business week days, I went with this simpler form, based on something I found online, don't remember where:
ISOWeekday = (0 == InputDate.getDay()) ? 7 : InputDate.getDay();
ISOCalendarWeek = Math.floor( ( ((InputDate.getTime() - (new Date(InputDate.getFullYear(),0,1)).getTime()) / 86400000) - ISOWeekday + 10) / 7 );
It fails in early January on days that belong to the previous year's last week (it produces CW = 0 in those cases) but is correct for everything else.

Subtracting days/months/years from a Date object

var inputDate = '20/4/2010'.split('/');
var dateFormatted = new Date(parseInt(inputDate[2]), parseInt(inputDate[1]), parseInt(inputDate[0]));
var expiryDate = (dateFormatted.getDate() - 1) + '/' + dateFormatted.getMonth() + '/' + (dateFormatted.getFullYear() + year);
This is the Javascript code I'm using to work out an expiry date given a user inputted date. Currently, the expiry date is original date minus one day and original year minus X.
The problems with this code, firstly, it doesn't take into account invalid dates. For example, if the user supplied date is '1/10/2010', the expiry date will be '0/10/2013' (assuming the expiry date is +3 years).
I could do something like:
var inputDate = '20/4/2010'.split('/');
var day = parseInt(inputDate[0]);
var month = parseInt(inputDate[1]);
var year = parseInt(inputDate[2]);
if (day < 1)
{
if (month == ...)
{
day = 31
month = month - 1;
}
else
{
day = 30
month = month - 1;
}
}
var dateFormatted = new Date(parseInt(inputDate[2]), parseInt(inputDate[1]), parseInt(inputDate[0]));
var expiryDate = (dateFormatted.getDate() - 1) + '/' + dateFormatted.getMonth() + '/' + (dateFormatted.getFullYear() + year);
But more problems arise... Firstly, the code gets a little convoluted. Secondly, this check would have to be done on the day. and then the month. Is there a cleaner, simpler way?
Also, there's a certain circumstance that would involve me needing to calculate the expiry date to the 'end of the month' for that date. For example:
Expiry date is: +3 years
User date is: '14/10/2010'
Expiry date is: '31/10/2013'
I was hoping the Date object would support these calculations but according to https://developer.mozilla.org/en/JavaScript/Reference/global_objects/date, it seems not...
Easy way to see if a date inputed is a valid date:
var d = Date.parse('4/20/2010');
if (isNaN(d.valueOf())) {
alert ("bad date value");
}
Then, here is a dateAdd function that I use regularly. Extends the Date object, so it's easy to use:
Date.prototype.dateAdd = function(size,value) {
value = parseInt(value);
var incr = 0;
switch (size) {
case 'day':
incr = value * 24;
this.dateAdd('hour',incr);
break;
case 'hour':
incr = value * 60;
this.dateAdd('minute',incr);
break;
case 'week':
incr = value * 7;
this.dateAdd('day',incr);
break;
case 'minute':
incr = value * 60;
this.dateAdd('second',incr);
break;
case 'second':
incr = value * 1000;
this.dateAdd('millisecond',incr);
break;
case 'month':
value = value + this.getUTCMonth();
if (value/12>0) {
this.dateAdd('year',value/12);
value = value % 12;
}
this.setUTCMonth(value);
break;
case 'millisecond':
this.setTime(this.getTime() + value);
break;
case 'year':
this.setFullYear(this.getUTCFullYear()+value);
break;
default:
throw new Error('Invalid date increment passed');
break;
}
}
Then just use:
var d = new Date();
d.dateAdd('day', -1).dateAdd('year', 3);
T'da
A similar question has been answered here:
How to add/subtract dates with javascript?
Similar thing can be done for months and years.
For e.g.
var date = new Date('2011','01','02');
alert('the original date is '+date);
var newdate = new Date(date);
newdate.setMonth(newdate.getMonth() - 7);
var nd = new Date(newdate);
alert('the new date is '+nd);
var currentDate = new Date(year,month,day);
var expiryDate = new Date();
expiryDate.setTime(currentDate.getTime() + (3 * 365 * 24 * 60 * 60 * 1000));
using the number of seconds past 1970 is fine for this :-)
oh, you have more rules. well after that you will still have to check for those cases...
Maybe this will be useful to you: http://code.google.com/p/datejs/wiki/APIDocumentation

Categories

Resources