0

I am working on a web form for IE.

I have my form completed, but I need the users to be able to only use the form in even weeks on Sundays after 6pm until 3pm on Monday (next day). This needs to be in EST no matter where the user is.

I haven't worked alot with Javascript and am at a complete loss on how to do this. What I am having the most trouble with is the determining today is in an even week for the year. (even weeks are pay weeks and this is only when the form should be used.)

I would like the today's date is in an even week criteria to be the first filter. I have written if, if else statements to determine if today's day and hours meet my criteria demands .getUTCDay() and .getUTCHours().

I was thinking of using an array of the qualifying Sunday and Monday dates but wasn't sure how to write the if statement for looking to see if today's date is in the array. Any help would be most appreciated.

CuberChase
  • 4,458
  • 5
  • 33
  • 52
Judy
  • 19
  • 2
  • 1
    On a side note, be sure to validate the form server-side as well! Client-side javascript can disabled by users to bypass the check. – thgaskell Mar 08 '13 at 00:49

2 Answers2

0

You can use the JavaScript Date object (See: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date) to see the current date, you can even parse it to whatever format you like. Then do your form logic.

For more elaborate Date manipulations, you can check out Moment.js http://momentjs.com/ it's a very comprehensive library for dealing with date/time.

Amy
  • 7,388
  • 2
  • 20
  • 31
0

Your first issue is to get a date object in the correct time. The timezone "EST" is used in at least 3 different parts of the world for different time zones, but I'll assume you want the US EST of UTC-05:00.

Javascript date objects have a UTC timevalue at their heart, so you can get a local date object, subtract 5 hrs, then use the UTC methods to get values for the equivalent US EST timezone. Once you have that date object, you just need to get the week number per the answer here except that you should use UTC methods instead of plain date methods (e.g. getUTCFullYear rather than getFullYear).

Note that this will ignore daylight saving time at the client location and that EST does not indicate whether daylight saving is in force or not. It's usually given a different time zone designator (e.g. places that use US EST and also daylight saving call their timezone EDT, which will be UTC-04:00).

Here is some code that should do the job, note that it's only lightly tested. I decided to include the local timezone offset to make it consistent and probably easier to understand.

// Returns true or false if current date is 
// an even week of the year in timezone UTC-5:00
// Adjust local date object to be UTC-5:00 object
// Provide a date object to test, or undefined for
// current date
function isPayWeek(d) {
  d = d? new Date(d) : new Date();

  // Adjust for local timezone and US EST in minutes
  d.setMinutes(d.getMinutes() + d.getTimezoneOffset() - 300);

  // return true if week number is even (note: zero is even)
  return !(getWeekNumber(d)[1] % 2)
}


function getWeekNumber(d) {
  d = new Date(d);
  d.setHours(0,0,0);
  d.setDate(d.getDate() + 4 - (d.getDay()||7));

  var yearStart = new Date(d.getFullYear(),0,1);
  var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)

  // Return array of year and week number
  return [d.getFullYear(), weekNo];
}

var d = new Date(2013,0,20,20,0,0); // 2013-01-20T10:00:00Z
var w = getWeekNumber(d); // week 3, 2013
alert(d + '\n' + 'week: ' + w + '\n' + isPayWeek(d)); // false

var d = new Date(2012,11,31,20,0,0); // 2012-12-31T10:00:00Z
var w = getWeekNumber(d); // week 1, 2013

alert(d + '\n' + 'week: ' + w + '\n' + isPayWeek(d)); // false

var d = new Date(2013,0,7,20,0,0); // 2013-01-07T10:00:00Z
var w = getWeekNumber(d); // week 2, 2013

alert(d + '\n' + 'week: ' + w + '\n' + isPayWeek(d)); // true
Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209