I have a date that comes from a MySQL database in the datetime format, something like 2010-09-24 11:30:12. And I need a way to show a countdown of hours:mins to that date.
I’m not very familiar with dates in JavaScript so any help would be apreciated. Thanks.
You may want to use the
UNIX_TIMESTAMP()function in MySQL to return your date in Unix Time Format (the number of seconds since January 1, 1970). Let’s say our target date is'2011-01-01 00:00:00'(in reality you would probably have a field from your table, instead of a constant):Then we can use the
getTime()method of theDateobject in JavaScript to calculate the number of seconds between the current time and the target time. Once we have the number of seconds, we can easily calculate the hours, minutes and days: