0

The code:

function format_date( date, index )
{
    if ( !date || ( index && !( date[ index ] ) ) )
    {
        return '';
    }

    console.log( date );

    var date = new Date(
        ( index === undefined ) ? date : date[ index ]
    );

    console.log( date );


    return ( date.getMonth() + 1 ) + '/' +
        date.getDate() + '/' +
        date.getFullYear() + ' 12:00 AM'
    ;
};

format_date( "2013-07-25" );

Output:

2013-07-25
Date {Wed Jul 24 2013 20:00:00 GMT-0400 (Eastern Standard Time)}
"7/24/2013 12:00 AM"

This is on a node JS server running on Linux. The output from date is:

Fri Aug  2 10:39:28 EDT 2013
4
  • 1
    And what is the problem? Maybe you are confused because the date parsed on new Date(...) is considered GMT, while you are using getDate() instead of getUTCDate() when manually parsing? Commented Aug 2, 2013 at 14:46
  • 2
    hey you might be interested in the time/date library moment.js to do this kind of stuff easily Commented Aug 2, 2013 at 15:19
  • @Plato That is an exceptionally cool library, but might be more than what I need. Commented Aug 2, 2013 at 15:41
  • 1
    This is just one of many things screwy with JS Date. Moment.js will make your life much easier. Even if you only use pieces of it. Commented Aug 2, 2013 at 16:28

1 Answer 1

2

Unless you specify a timezone offset, it assumes that your date is in GMT time.

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

1 Comment

True, but only because the string is entered with dashes as separators. If it was provided as "2013/07/25" instead, then it would be interpreted as local time. All the better to use moment.js

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.