View Full Version : Resolved Help to retrieve selected date using Epoch calendar

06-18-2010, 01:52 AM
Hi all,

I'm using the Epoch calendar at:


It works great and does exactly what I need. I'm using it in flat mode so that it can be displayed statically on the page. What I can't seem to work out is how to retrieve the selected date once clicked on. I need to be able to print the output in dd/mm/yyyy format, either on screen or in an alert or something similar.

If anyone has worked with this calendar and knows how to do it, or can sort it, I'd appreciate it greatly. I can't paste code here as it's too long, but the source code on the site linked above is exactly the same as I'm using.


Old Pedant
06-18-2010, 02:31 AM
It looks to me like you can get the clicked on date by looking at the selectedDates property of the calendar you create.

That is, if you do

var mycalendar = new Epoch(....);

then you can use

var dates = mycalendar.selectedDates;

Note that this will always be an array, even if there is only one date allowed/chosen (that's because the general calendar will handle multiple dates).

So if you didn't ask for multiple dates, you could do

var pickedDate = mycalendar.selectedDates[0];

Or, better:

var pickedDate = mycalendar.selectedDates.length > 0 ? mycalendar.selectedDates[0] : null;

So that the value is null if no date has yet been chosen.

** I HAVE NOT TESTED THIS. ** I just got that by reading the code.

06-18-2010, 02:57 AM
OP, you da man!! (as usual).

Your suggestion worked great. The only thing I have to figure out now is to format the date into the dd/mm/yyyy format I need. At the moment it prints in the format: Thu Jun 17 00:00:00 UTC+1000 2010

I appreciate the quick response mate.

Old Pedant
06-18-2010, 03:26 AM
That means that it is *probably* an instance of a JavaScript Date object.

So you can use the conventional ways of formatting it:

var dt = mycalendar.selectedDates.length > 0 ? mycalendar.selectedDates[0] : null;
if ( dt != null )
var prettyDate = dt.getDate() + "/" + ( dt.getMonth() + 1) + "/" + dt.getFullYear();

If you insist on two digits for day an month it's only a bit more code:

if ( dt != null )
var dy = dt.getDate(); if ( dy < 10 ) dy = "0" + dy;
var mn = dt.getMonth() + 1; if ( mn < 10 ) mn = "0" + mn;
var prettyDate = dy + "/" + mn + "/" + dt.getFullYear();

or similar code.

06-18-2010, 03:55 AM
Perfect once again!! Thanks. Oh, and also thanks for including the "on purpose" typo in the code above, to keep me on my toes!!

You're a legend.

Old Pedant
06-18-2010, 07:46 PM
LOL! It wasn't on purpose! It's just what comes from touch typing. - and = are sise by sise [yeah, that's on purpose].