1

I am currently looking to calculate a custom date in JavaScript and my head is beginning to hurt thinking about it. I have a countdown clock that is to start every other Tuesday at 12pm. I have the countdown function working properly using the jQuery countdown plugin by Keith Wood but need assistance in calculating every other Tuesday of the month and having it reset on this day.

All help is greatly appreciated as always.

Thansk in advance

jeffreynolte
  • 3,749
  • 11
  • 41
  • 64
  • 3
    What code do you have so far? – qwertymk Jan 27 '11 at 02:41
  • The .getDay() function might help: http://www.w3schools.com/js/js_obj_date.asp – Mike Robinson Jan 27 '11 at 02:58
  • hi jnolte :) please try to post some of your codes, it might help us solve your problem faster. tnx! :) – Carls Jr. Jan 27 '11 at 03:16
  • You mean to say that on dates `Jan/1/11` thru `Jan/10/11 23:59:59` it'll show seconds till `Jan/11/11 0:0:0`, from then on it'll show seconds till `Jan/25/11 0:0:0`? If so, what happens on and after `Jan/25/11 0:0:0`? – Salman A Jan 27 '11 at 07:31
  • I read this as meaning on Jan/25/11 and every fortnight thereafter, display a countdown to some unspecified event occuring at some unspecified time, for the duration of that day. e.g. counting down to scheduled mainainance – tobyodavies Jan 27 '11 at 08:34

4 Answers4

1

Please find attached link for Date Library to get the custom calculation date and time functions.

To use it client side, download index.js and assertHelper.js and include that in your HTML.

<script src="assertHelper.js"></script>
<script type="text/javascript" src="index.js"></script>
$( document ).ready(function() {
    DateLibrary.getDayOfWeek(new Date("2015-06-15"),{operationType:"Day_of_Week"}); // Output : Monday
}

You can use different functions as given in examples to get custom dates.

To get First Day of quarter From Given Date

DateLibrary.getRelativeDate(new Date("2015-06-15"),
    {operationType:"First_Date",granularityType:"Quarters"}) // Output : Wed Apr 01 2015 00:00:00

If first day of week is Sunday, what date will be on Wednesday, if given date is 15th June 2015

DateLibrary.getRelativeDate(iDate,
    {operationType: "Date_of_Weekday_in_Week",
        startDayOfWeek:"Sunday",returnDayOfWeek:"Wednesday"}) // Output : Wed Jun 17 2015 00:00:00

If first day of week is Friday, what date will be on Tuesday of 3rd Week of 2nd month of 3rd quarter of year containing 15th June 2015 as one of the date.

DateLibrary.getRelativeDate(new Date("2015-06-15"),
    {operationType: "Date_of_Weekday_in_Year_for_Given_Quarter_and_Month_and_Week",
        startDayOfWeek:"Friday",returnDayOfWeek:"Tuesday", QuarterOfYear:3, MonthOfQuarter:2, WeekOfMonth:3}) // Output : 18th Aug 2015

If first day of week is Tuesday, what week number in year will be follow in 15th June 2015 as one of the date.

 DateLibrary.getWeekNumber(new Date("2015-06-15"),
    {operationType:"Week_of_Year",
        startDayOfWeek:"Tuesday"}) // Output : 24

There are Date Difference functions also available

 DateLibrary.getDateDifference(new Date("2016-04-01"),new Date("2016-04-16"),
    {granularityType: "days"}) //output 15

Function for Convert number to Timestr

DateLibrary.getNumberToTimeStr("345", {delimiter: ":"}) //output 00:03:45

It also supports Julian date conversion

 DateLibrary.julianToDate("102536") //output Fri Jun 20 2003 00:00:00
Vikash Rajpurohit
  • 1,525
  • 2
  • 13
  • 13
1

I've had to do something similar (not in JS but the algorithm is similar enough)

Now, before i start, to clarify i'm assuming this is something that happens fortnightly regardless of the length of the month, and not on the second and 4th Tuesday regardless of when it last happened, which is simpler to solve

Pick a date in the past that this event has occured on (or the date of the first occurrence) , we'll call this date base in the following code

var base=new Date('date of first occurrence');
var one_day=1000*60*60*24; //length of day in ms

// assume we care about if the countdown should start today 
// this may be different if you are building a calendar etc.
var date_to_check=new Date();

var diff_in_days=math.floor(date_to_check-base)/one_day);
var days_since_last_reset= diff_in_days%14;
if(days_since_last_reset == 0){
    //date_to_check is the same day in the fortnightly cycle as base
    //i.e. today at some point is when you'll want to show the timer
    //If you only want to show the timer between certain times,
    //add another check here
}else{
    //next reset in (14 - days_since_last_reset) days from date_to_check
}

Or the code-golf-esque version:

if( Math.floor((new Date()-new Date('date of first occurrence'))/1000/60/60/24)%14 == 0 )
    //reset/start timer
tobyodavies
  • 27,347
  • 5
  • 42
  • 57
0

There is a JavaScript implementation of RFC 2445 recurrence rules : http://code.google.com/p/google-caja/source/browse/trunk/src/com/google/caja/demos/calendar/rrule-cajita.js which requires some files in the same directory. See the unit test ( http://code.google.com/p/google-caja/source/browse/trunk/tests/com/google/caja/demos/calendar/rrule_test.js ) for examples of how it works.

Try using it to parse RRULE:FREQ=WEEKLY;BYDAY=TU;INTERVAL=2 which means every second (because of the interval) week (because of the frequency) on Tuesday (because of the byday).

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Thats cool, I've no experience with RFC recurrences, how do you specify a start date for this? As in my mind the English description describes 2 different recurrences - one that includes 2011-01-04 and one that includes 2011-01-11 – tobyodavies Jan 27 '11 at 07:29
  • The start date is the `dtStart` passed to `rrule.createRecurrenceIterator(new StubContentLine(rruleText), dtStart, tz)` at line 47 of the unittests. It can be specified using either a date format (`time.parseIcal("20060120")`) or a date-time format (`time.parseIcal("20060120T123000")`). If you want to specify only 2 recurrences, that's specified using `;COUNT=2`, not `;INTERVAL=2`. – Mike Samuel Jan 27 '11 at 16:04
  • two events != two recurrences - i meant that it specified two distinct sequences, and based on your last comment, the distinction between these sequences is made at the time of constructing an iterator, not in the specification itself... – tobyodavies Jan 28 '11 at 00:29
  • Sorry. Maybe I don't understand. In "Every other Tuesday of the month", I assumed you meant fortnightly starting from a particular Tuesday. Is that not the case? The code I linked to does allow multiple recurrences. Just pass in 2 or more RRULEs. – Mike Samuel Jan 28 '11 at 01:16
  • 1) its not my question, 2) i'm saying that what i meant in my very first comment by you describing 2 recurrences you then took to mean two events. i.e. you and i are thinking the same thing, however i was unsure how to specify which of the two possible sequences to use, something you explained in the same comment (its an argument to the constructor, separate from the recurrence rule), but it seemed like you interpreted my pharse "two different occurences" to mean "this recurrance should stop after two events" which it didn't. – tobyodavies Jan 28 '11 at 03:42
0

Have a look at the date.js library. It has several date parsing helpers including Date.today().next().tuesday() (among others).

Paul Alexander
  • 31,970
  • 14
  • 96
  • 151