Facebook Twitter Gplus LinkedIn RSS
 
 
Home » Blog » Manipulating the Javascript Date with DateJS

Manipulating the Javascript Date with DateJS

Published on May 10th, 2012

In one of my apps currently in development, we are using calendar reminders on the iPhone. This involves determining the current date, and trying to calculate such things as the next time that “the first Monday of the month” will occur. Javascript has a basic Date object that has a lot of functionality right out of the box. Invoke it with a very simple:

1
var todayDate = new Date();
var todayDate = new Date();

Now you have an object that you can manipulate with simple functions like:

1
2
3
todayDate.getDay();
todayDate.getMonth();
todayDate.getDay();
todayDate.getDay();
todayDate.getMonth();
todayDate.getDay();

There are a ton of different functions you can invoke to get basic calendar information. The browser (through the OS) handles all of the leap year calculations and such. However, for my project, I needed to be able to find things like “the third Wednesday of the month”. There is no simple javascript function for this. After a little bit of searching, I stumbled upon DateJS. This is a full framework for manipulating the Date object. It is actually pretty dang cool.

Download the latest version from the SVN. For the US, you will want the date-en-US.js file. Include it in the header of your html.

1
<script type="text/javascript" src="date-en-US.js"></script>
<script type="text/javascript" src="date-en-US.js"></script>

After you do that, you can do fun stuff like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Get today's date
Date.today();
 
// Add 5 days to today
Date.today().add(5).days();
 
// Get Friday of this week
Date.today().friday();
 
// Is today Friday?
Date.today().is().friday();  // true|false
 
// What day is it?
Date.today().getDayName();
// Get today's date
Date.today();
 
// Add 5 days to today
Date.today().add(5).days();
 
// Get Friday of this week
Date.today().friday();
 
// Is today Friday?
Date.today().is().friday();  // true|false
 
// What day is it?
Date.today().getDayName();

In my case, I used the following to find the next upcoming first Sunday of the month.

1
2
3
4
5
6
today = Date.today();   
next = Date.today().first().sunday();   
if (today >= next) {
    next = Date.today().add(1).month().first().sunday();
}
alert(next);
today = Date.today();	
next = Date.today().first().sunday();	
if (today >= next) {
    next = Date.today().add(1).month().first().sunday();
}
alert(next);

The above code says, grab today’s date and find the first Sunday in the month. If that date is older than today’s date, then go to next month, and try again.

About the Author: Sprawl

Stephen Russell is a Mobile App developer and all around IT geek that spends his days running data centers and his nights coding. This site is the go to place for all of zSprawl's work and the infamous development blog. In his free time, he enjoys tinkering with web code, playing video games, and otherwise plotting to take over the Internets.

 

Leave a Reply

© 2012 zSprawl's zApps

Fork me on GitHub