试图阅读文档,但似乎找不到两者之间的区别(如果有的话)。它们似乎都接受相同的参数和语言环境,并且似乎返回相同的值。
它们只是相同功能的别名吗?还是它们之间实际上有区别?
const locale = 'no-nb'
const options = {
day: '2-digit', month: 'long',
hour: '2-digit', minute: '2-digit'
}
new Date().toLocaleString(locale, options)
"18. mai, 15"
new Date().toLocaleDateString(locale, options)
"18. mai, 15"
new Date().toLocaleTimeString(locale, options)
"18. mai, 15"
答案 0 :(得分:3)
如果您为至少一个日期元素(day
,month
,year
)和至少一个时间提供自定义格式,则所有这些对象都将得到完全相同的结果元素(hour
,minute
,second
)。
它们的目的和默认行为不同。尝试跳过所有自定义时间格式,例如:
new Date().toLocaleString('no-nb', {day: '2-digit'})
// 18
new Date().toLocaleDateString('no-nb', {day: '2-digit'})
// 18
new Date().toLocaleTimeString('no-nb', {day: '2-digit'})
// 18, 15:37:37
如您所见,toLocaleTimeString()
总是在其中放置时间,如果未指定,则使用默认时间格式。
toLocaleDateString()
做同样的事情,只是日期而不是时间:
new Date().toLocaleString('no-nb', {hour: '2-digit'})
// 15
new Date().toLocaleDateString('no-nb', {hour: '2-digit'})
// 18.05.2020, 15
new Date().toLocaleTimeString('no-nb', {hour: '2-digit'})
// 15
toLocaleString()
允许您以自己喜欢的方式设置日期格式,不会增加任何多余的内容。
答案 1 :(得分:0)
因为所有方法都接受Intl.DateTimeFormat作为构造字符串的选项,所以对于相同的选项,所有方法都将返回相同的字符串。
但是,所有三个文档的var BaseDialog = new function () {
this.foobar = function () {
// get the prototype function name that called me? E.g DialogOne
}
}
DialogOne.prototype = BaseDialog;
function DialogOne() {
this.foobar();
}
DialogTwo.prototype = BaseDialog;
function DialogTwo() {
this.foobar();
}
文档都清楚地提到了if (message.content.startsWith(`!test`)) {
await message.channel.send('Hello').then(r => r.delete({ timeout: 5000 }))
console.log("test");
}
,但方法的意图有所不同,应按建议使用。
文档还说ECMA
允许的选项如下
toLocaleString => The contents of the String are implementation-dependent
from
toLocaleDateString => implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
from
toLocaleTimeString => "any", "all"
from