0

When I generate a date with the following code

var date = new Date();

I get a date-time of the following form

Sat May 11 2013 21:54:23 GMT-0700 (PDT)

Can anyone tell me how to generate a date of the form below without using regex/string functions.

Sat May 11 2013 21:54:23
1
  • The formatting of new Date is implementation dependent, so you'd be better off making up your own string from the UTC epoch timestamp. Why don't you want to (or can't) use regular expressions/string formatting? Commented May 12, 2013 at 5:51

2 Answers 2

1

Try giving this a shot: http://blog.stevenlevithan.com/archives/date-time-format

It has a collection of Date format options that I believe would give you what you desire.


Ignoring the above Javascript library and doing it native:

var date = new Date();
date = date.toDateString() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();

Does this work ok for you?

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Adam. I'd love to achieve this with out adding a library.
Oh! I think I've found something for you: jsfiddle.net/3ZPXa Here is the stackoverflow link for more info stackoverflow.com/questions/3184618/… edit: I just noticed it didn't include your hour,min and second requirement so I updated the fiddle and and here is the link jsfiddle.net/3ZPXa/18
1

Javascript does not come with a date formatting library beyond the one specific format you see. You can either build your own by piecing together the exact pieces you want and adding the strings together or you can get a third party date formatting library.

If you want a 3rd party library, the Datejs library is pretty thorough. In that library, it would be:

Date.today().toString("ddd MMM d yyyy H:mm:ss");

You could, of course obtain all the component values from the built-in date object and then build your own string too.

Without adding a library, you'd have to write your own way to make that specific format:

function formatDate(date) {
    function makeTwoDigits(val) {
        var prefix = val <= 9 ? "0" : "";
        return prefix + val;
    }
    var dayOfWeek = date.getDay();                  // 0-6, 0=Sunday
    var month = date.getMonth();                    // 0-11
    var day = date.getDate();                       // 1-31
    var year = date.getFullYear();                  // 2013
    var hours = makeTwoDigits(date.getHours());     // 0-23
    var mins = makeTwoDigits(date.getMinutes());    // 0-59
    var secs = makeTwoDigits(date.getSeconds());    // 0-59

    var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

    return days[dayOfWeek] + " " + months[month] + " " + day + " " + year + " " +
           hours + ":" + mins + ":" + secs;
}

Working demo: http://jsfiddle.net/jfriend00/zu7Uz/

2 Comments

@benpearce - I added a custom function to produce the desired date format.
@benpearce - Updated my function to pad leading zeroes where that is normally done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.