1

have problem for format date in JavaScript, this is my function code

   //originalDate = '2016-03-02 09:12:14.989522';
   var d = new Date(originalDate),
        month = d.getMonth() + 1,
        day =d.getDate(),
        year = d.getFullYear(),
        hour = d.getHours(),
        min = d.getMinutes();
    alert([day, month, year].join('-')+' '+[hour,min].join(':'));

and my original date ='2016-03-02 09:12:14.989522'; and my code always return 'Nan-Nan-Nan Nan:Nan', It's seen unknown originalDate that I pass to. any help?

Note: datatype in database of date of mine is timestamp

15
  • 2
    The code you posted works fine, if date is defined correctly. You're probably not passing in the right variable to new Date(date). If you paste the full code it will be more obvious what the problem is. Commented Mar 2, 2016 at 6:06
  • can you print date in the console before running these statements .. console.log(date); Commented Mar 2, 2016 at 6:07
  • I doubt new Date(date) returns valid date object.. Commented Mar 2, 2016 at 6:07
  • Can you do console.log(date) at the beginning just to check if you are getting the same value you posted? Commented Mar 2, 2016 at 6:08
  • Test the date value given in your question, I got "2-3-2016 9:12" Commented Mar 2, 2016 at 6:10

3 Answers 3

1

Simple solution: Replace the space in your date string with a "T".

(However, to be completely technically correct, you should also include a time zone indicator at the end, either an additional "Z" to indicate UTC, i.e. Coordinated Universal Time, or "+hh:mm" or "-hh:mm" to indicate a time zone offset.)

The MDN site for Date.parse() writes:

Because of the variances in parsing of date strings, ...results are inconsistent, especially across different ECMAScript implementations where strings like "2015-10-12 12:00:00" may be parsed to as NaN, UTC or local timezone.

and

The date time string may be in ISO 8601 format.

The ISP 8601 specs referred to above writes:

The formats are as follows. Exactly the components shown here must be present, with exactly this punctuation. Note that the "T" appears literally in the string, to indicate the beginning of the time element, as specified in ISO 8601.

and

Complete date plus hours, minutes, seconds and a decimal fraction of a second YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

Here is your code re-written replacing the space in date with a "T". If that doesn't work in your browser, add a "Z" or time zone offset at the end of date.

var date ='2016-03-02T09:12:14.989522';
var d = new Date(date),
  month = d.getMonth() + 1,
  day = d.getDate(),
  year = d.getFullYear(),
  hour = d.getHours(),
  min = d.getMinutes();
document.write([day, month, year].join('-') + ' ' + [hour, min].join(':'));

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

1 Comment

Note that "just adding a T" means that where the string is parsed successfully, it will be treated as a local date, however if a "Z" as appended, it will be treated as UTC. Also, if it is a date only without a timezone, it will be treated as UTC. Otherwise, the result will be an invalid date. Just so we're clear. :-)
1

Is the date parameter in your code a Date object? That won't work. There is no such constructor in Javascript. You could use date.now() though.

Check here for the valid constructors for Date https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date

1 Comment

No, the date parameter is the questioner's code is a string, and it is (almost) valid. A direct quote from the site you linked to: var birthday = new Date('1995-12-17T03:24:00'); where the '1995-12-17T03:24:00' part is a dateString. See my answer about the use of the "T" in that string, though, which seems to be the solution.
0

First parse your original date then use it in your code.

var MilliSecond=Date.parse(originalDate);
var d=new Date(MilliSecond),
month=d.getMonth()+1,
day=d.getDay(), .......

2 Comments

In a browser where the dateString in the original question produces an error (e.g. my browser, i.e. Firefox v44.0.2), using Date.parse(date) does not solve the problem, i.e. it still produces NaN errors everywhere.
You stated your original date as //originalDate = '2016-03-02 09:12:14.989522'; Replace the space between date and time with letter T then parse the date string. e.g. originalDate='2016-03-02T09:12:14.989522';

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.