1

I am displaying a date time object in a table however for some reason in IE it display as None or Invalid Date is there something wrong with my format or is there an easy way for making this more readable such as mm/dd/yyyy HH:MM

this is what displays in chrome:

Mon Nov 28 2011 16:00:00 GMT-0500 (EST)

This is being converted from a Unix timestamp to that output in an API layer.

1
  • 2
    Could we please see your code? Dec 9, 2011 at 21:46

3 Answers 3

2

Probably the creation of the Date object fails, because the new Date() constructor accepts just some limited, implementation-dependent set of date strings.

You can use the Globalize library to deal with such issues, even if no localization in the usual sense is involved—but dealing with different string presentations of dates as is localization of a kind. It first looks a bit messy (it takes some time to dig into it—my book “Going Global with JavaScript and Globalize.js” contains a more readable description of it, with many examples), and it’s far from perfect, but it’s very useful.

If you know that your timestamp data is of some known exact format, you can parse it easily and then output it according to your own format descriptor. Assuming, for the sake of definiteness, that the format is the one exemplified with

Mon Nov 28 2011 16:00:00 GMT-0500 (EST)

(I know it’s an output format you mentioned, but I just use it as an example), you would first do simple string operation to discard the “GMT” and “(EST)” part (Globalize cannot currently handle them), producing e.g.

Mon Nov 28 2011 16:00:00 -05:00

and then you would just use code like the following:

var foo = Globalize.parseDate(timestamp,'ddd MMM d yyyy HH:mm:ss zzz');
var out = Globalize.format(foo,'MM/dd/yyyy HH:MM');
document.write(out);
0

just make your own method to format the date as string so you pass all problems with diffrent browsers and plateforms

-1

I suspect Chrome is being helpful here and calling the .toString() method for you.

The Date object has several methods for formatting string output. See the w3schools reference page for examples.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.