1

How do I format a date in Javascript to something e.g. 'yyyy-MM-dd HH:mm:ss z'?

This date.toString('yyyy-MM-dd HH:mm:ss z'); never work out for me :/

Any idea?

======

I solved my own which I rewrote like this:

var parseDate = function(date) {
    var m = /^(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d) UTC$/.exec(date);
    var tzOffset = new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]).getTimezoneOffset();

    return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5] - tzOffset, +m[6]);
}

var formatDateTime = function(data) {
    var utcDate = parseDate(data);

    var theMonth = utcDate.getMonth() + 1;
    var myMonth = ((theMonth < 10) ? "0" : "") + theMonth.toString();

    var theDate = utcDate.getDate();
    var myDate = ((theDate < 10) ? "0" : "") + theDate.toString();

    var theHour = utcDate.getHours();
    var myHour = ((theHour < 10) ? "0" : "") + theHour.toString();

    var theMinute = utcDate.getMinutes(); 
    var myMinute = ((theMinute < 10) ? "0" : "") + theMinute.toString();

    var theSecond = utcDate.getSeconds(); 
    mySecond = ((theSecond < 10) ? "0" : "") + theSecond.toString();

    var theTimezone = new Date().toString();
    var myTimezone = theTimezone.indexOf('(') > -1 ? 
            theTimezone.match(/\([^\)]+\)/)[0].match(/[A-Z]/g).join('') : 
            theTimezone.match(/[A-Z]{3,4}/)[0];

    if (myTimezone == "GMT" && /(GMT\W*\d{4})/.test(theTimezone)) {
        myTimezone = RegExp.$1;
    }

    if (myTimezone == "UTC" && /(UTC\W*\d{4})/.test(theTimezone)) {
        myTimezone = RegExp.$1;
    }

    var dateString = utcDate.getFullYear() + "-" + 
                     myMonth + "-" +
                     myDate + " " +
                     myHour + ":" +
                     myMinute + ":" +
                     mySecond + " " +
                     myTimezone;

    return dateString;
}

and I get: 2012-11-15 22:08:08 MPST :) PERFECT!

8
  • possible duplicate of how to display a date as 2/25/2007 format in javascript, if i have date object Commented Nov 29, 2012 at 9:52
  • It's not an exact duplicate but it's the same problem and you should figure it out from there. Commented Nov 29, 2012 at 9:53
  • i read that one, some used external scripts. i want something pure with function. Commented Nov 29, 2012 at 9:53
  • Only one answer provided an external library. Others use pure JavaScript. Commented Nov 29, 2012 at 9:54
  • i try not to do substring or string.format stuffs :/ Commented Nov 29, 2012 at 9:54

3 Answers 3

1
function formatDate(dateObject) //pass date object
{
  return (dateObject.getFullYear() + "-" + (dateObject.getMonth() + 1)) + "-" + dateObject.getDate()   ;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Use this lib to make your life much easier:

var formattedDate = new Date().format('yyyy-MM-dd h:mm:ss');
document.getElementById("time").innerHTML= formattedDate;

DEMO

Comments

0

Basically, we have three methods and you have to combine the strings for yourself:

getDate(): Returns the date

getMonth(): Returns the month

getFullYear(): Returns the year

Example:

var d = new Date();

var curr_date = d.getDate();

var curr_month = d.getMonth() + 1; //Months are zero based
var curr_year = d.getFullYear();
document.write(curr_date + "-" + curr_month + "-" + curr_year); </script>

for more details look at 10 steps to format date and time and also check this

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.