JavaScript Date.toString()基数参数错误 [英] JavaScript Date.toString() radix argument error
问题描述
为什么以下代码不会将我的日期输出到字符串!?
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'));
错误是:
未捕获RangeError:toString()基数参数必须介于2和
36之间
Uncaught RangeError: toString() radix argument must be between 2 and 36
只是尝试格式化日期......
Just trying to format the date...
推荐答案
因为 d1
不是 日期
object ,但是一个数字。 Date.parse
返回毫秒表示,您需要将其输入新日期
或使用日期
构造函数直接。
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.
由于JavaScript没有本机日期格式化函数,因此只有依赖于实现的 toString
和 toLocalString
和标准化的 toISOString
和 toUTCString
(虽然在较旧的IE中不受支持)。相反,您必须通过获取单个组件并连接它们来手动进行格式化。幸运的是,有一堆图书馆可以帮助你。
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.
这篇关于JavaScript Date.toString()基数参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!