4

Why won't the following code output my date to string!?

var d1 = Date.parse('10/29/1990 12:00:00 AM');
console.log(d1.toString('dd/MM/yyyy'));

The error is:

Uncaught RangeError: toString() radix argument must be between 2 and 36

Just trying to format the date...

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • 1
    JavaScript dates can't be formatted that way unless you're trying to use some fancy add-on library (in which case, it doesn't appear to be working :-) – Pointy May 13 '13 at 16:45
  • 2
    have you tried [datejs](http://www.datejs.com/) or are you using it allready? – Jan Hommes May 13 '13 at 16:46
  • @Pointy I was looking at http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript How do you recommend I format the date? – user1477388 May 13 '13 at 16:46
  • @JanHommes no, but honestly; I've worked with javscripts datetime object before and it didn't seem like a problem before. I don't want to use an entire library for one new line of code :) – user1477388 May 13 '13 at 16:47
  • @user1477388 Read the comments on that question: the OP was mistaken about the formatting function. You either need to use a library or build the string from individual parts, like in the accepted answer of that question. – JJJ May 13 '13 at 16:48
  • 2
    The answer you are linking to uses the [date time format library](http://blog.stevenlevithan.com/archives/date-time-format), without it that wont work. – adeneo May 13 '13 at 16:48
  • 1
    I would recommer it. Datejs is really helpfull, excspecially if it comes to globalization. Have you tried 'toLocaleString()'? – Jan Hommes May 13 '13 at 16:51
  • I think I am just going to use `myDateStr.split(' ')[0]`. That seems to work for now. Gives me 10/29/1990. – user1477388 May 13 '13 at 17:00
  • 1
    @user1477388: But didn't you want to swap days and months? Otherwise `.slice(0, 10)` would be enough of course… – Bergi May 13 '13 at 19:04
  • @Bergi You're right, but I only said that mistakenly. 10/29/1990 is what I wanted (that is the US format). Thanks. – user1477388 May 13 '13 at 19:13

2 Answers2

9

Because d1 is not a Date object, but a number. Date.parse returns the milliseconds representation, you will need to feed that into new Date or use the Date constructor directly.

And because JavaScript does not have a native date-formatting function, there are only the implementation-dependent toString and toLocalString and the standardized toISOString and toUTCString (though not supported in older IE). Instead, you will have to do the formatting manually by getting the single components and concatenating them. Luckily, there's a bunch of libaries to help you with that.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

The JavaScript Date.prototype.toString method takes no parameters (like formatting and such).

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toString

Using Date.prototype.toLocaleDateString might help: d1.toLocaleDateString('en') works for me, but check out all the fine-print here:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toLocaleString

Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
freethejazz
  • 2,235
  • 14
  • 19