简体   繁体   中英

JavaScript: Convert Day/Week Into Year

I have been using Stack Overflow for a number of months now, but this is my first post.

I require a function to convert a week number and and day of week into a dd/mm/yyyy format.

The date values i have to work with are in the format day/weekNumber . So for example: 3/43 converts to Wednesday 24 October 20XX. The year value will be the current year.

The day value starts at 1 (Monday).

I have found lots of functions on the internet (such as this , this and this ). Some work with ISO 8601 dates, which i do not think will work for me. And i have not yet found one that works for me.

Thanks in advance,

So assuming you have the values of 3 and 43 separately, you can just do some simple maths on the first day of the current year:

  • Get 1st January Current Year
  • Add (43 * 7 + 3)

Something like this maybe:

var currentDate = new Date();
var startOfYear = new Date(currentDate.getFullYear(), 0, 1);//note: months start at 0
var daysToAdd = (43 * 7) + 3;
//add days
startOfYear.setDate(startOfYear.getDate() + daysToAdd);

Here is an example


EDIT

On second thoughts, I think I was wrong with your requirements. It seems you require a specific day of the week. Check this out for a better solution.

The problem is that it all depends on your definition of a week. This year starts on a sunday, so does that mean that 02/01/2012 (the first monday of this year) is the start of the second week?

My latest example will first find the start of the specified week, and then find the next occurrence of the specified day

This solution does require an extra library to be added, but I think it is really worth it. It is a momentjs library for manipulating dates and time. It is actively maintained and has a great documentation . Once you get the values for day and weekNumber (in our case 3 and 43), you should do as follows:

function formatInput(day, weekNumber){

    var currentDate = moment(new Date());     // initialize moment to a current date
    currentDate.startOf('year');              // set to Jan 1 12:00:00.000 pm this year
    currentDate.add('w',weekNumber - 1);      // add number of weeks to the beginning of the year (-1 because we are now at the 1st week)
    currentDate.day(day);                     // set the day to the specified day, Monday being 1, Sunday 7

    alert(currentDate.format("dddd, MMMM Do YYYY"));  // return the formatted date string 
    return currentDate.format("dddd, MMMM Do YYYY");
}

I think this library might be useful to you later on and there are plenty of possibilities regarding date and time manipulation, as well as formatting options. There is also a great documentation written for momentjs.

According to ISO when dealing with week dates, the week starts on Monday and the first week of the year is the one that contains the first Thursday of the year. So for 2012, the first week started on Monday, 2 January and the first week of 2013 will start on Monday, 31 December 2012.

So if 3/43 is the third day of the 43rd week (which is the ISO date 2012-W43-3), then it can be converted it to a date object using:

function customWeekDateToDate(s) {
  var d, n;
  var bits = s.split('/');

  // Calculate Monday of first week of year this year
  d = new Date();   
  d = new Date(d.getFullYear(),0,1); // 1 jan this year
  n = d.getDay();
  d.setDate(d.getDate() + (-1 * n +( n<5? 1 : 8)));

  // Add days
  d.setDate(d.getDate() + --bits[0] + --bits[1] * 7);

  return d;
}

console.log(customWeekDateToDate('3/43')); // 01 2012-10-24

Note that this uses dates, otherwise daylight saving changeovers may result in the wrong date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM