1

I'm trying to display a date with format "MMM. dd HH:mm:ss.nnn". It is rendering it incorrectly in IE and I have spent quite some time and I can't figure out why I can't get this to work.

I know that Date.UTC returns the number of miliseconds in a Date object since Jan 1, 1970. So,

var newDate = new Date(Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]])
newDate.toString("MMM. dd HH:mm:ss.")+row.timestamp.getMilliseconds();

will work.

Example:

var newDate = new Date(Date.UTC(1950, 10, 10, 10, 09, 09, 100));
row.timestamp_f = newDate.toString("MMM. dd HH:mm:ss."); // Output => Nov. 10 05:09:09.

But, I am interating this from a jquey.each function so the date string that I am working with is an ISO 8601: "2013-03-12T15:14:10.483". So, this is what I have in mind.

var numMilisecond = Date.parse(row.timestamp);
var newDate = new Date(numMilisecond);
row.timestamp_f = newDate.toString("MMM. dd HH:mm:ss."); // Output => Dec. 31 19:00:00.

row.timestamp is from a JSON response

{"timestamp":"2013-03-12T15:14:10.483" ...}

Why doesn't the code work? Date.parse should return the number of miliseconds since Jan 1, 1970 and then I create a new Date obj and then convert it to string just like the code in the first snipet. What am I doing wrong?

Thanks.

James M
  • 18,506
  • 3
  • 48
  • 56
oky_sabeni
  • 7,672
  • 15
  • 65
  • 89

1 Answers1

0

Date.toString shouldn't accept any arguments. If you want a true date-formatting solution, you'll need to use a plugin or roll your own.

var shortmonths = ['Jan','Feb','Mar','Apr',]; // remaining months are left as an exercise for the reader
row.timestamp_f = shortmonths[newDate.getMonth()]
                  + ". "+newDate.getDate() + " "
                  + newDate.toLocaleTimeString() + ".";
Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • So I am totally embarrassed but it turns out there is a datejs which is doing a lot of magic in the background – oky_sabeni Mar 13 '13 at 16:50