toLocaleDateString() short format
toLocaleDateString() short format
我想使用Date.toLocaleDateString()的简短表示法,但采用本地格式。有很多解决方案可以硬编码 yyyy-mm-dd 格式,但我希望它依赖于托管页面的系统。这是我到目前为止的函数:
function getDate(dateTimeString)
{
var date = getDateTime(dateTimeString);
var options = { year: "numeric", month: "numeric", day: "numeric" };
return date.toLocaleDateString( date.getTimezoneOffset(), options );
}
但这返回如下:2015 年 1 月 28 日,星期三,我不想要。有什么建议/想法吗?
PS:它不是浏览器,使用它的人很有可能没有互联网连接; 所有信息都是从本地数据库中获取的,所以我不能使用任何像这样的东西 如何使用javascript地理位置获取访问者的位置(即国家)。
toLocaleDateString使用设备上的默认本地数据。
尝试以下代码来检查输出:
// America/Los_Angeles for the US
// US English uses month-day-year order
console.log(date.toLocaleDateString('en-US'));
// → "12/19/2012"
// British English uses day-month-year order
console.log(date.toLocaleDateString('en-GB'));
// → "20/12/2012"
// Korean uses year-month-day order
console.log(date.toLocaleDateString('ko-KR'));
// → "2012. 12. 20."
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleDateString('ar-EG'));
// → "٢٠/١٢/٢٠١٢"
// chinese
console.log(date.toLocaleDateString('zh-Hans-CN'));
// → "2012/12/20"
JavaScript Date toLocaleString()此方法使用特定于语言的格式将日期格式化为字符串。
例子:
仅日期 :
var n = new Date();
console.log("es-CL: " + n.toLocaleDateString("es-CL"));
// es-CL: 03-09-2021
日期与时间 :
var n = new Date();
console.log("es-CL: " + n.toLocaleString("es-CL"));
// es-CL: 03-09-2021 17:56:58
列表在这里 :
ar-SA: ٢٦/١/١٤٤٣ هـ في ٥:٥٦:٥٨ م
bn-BD: ৩/৯/২০২১ ৫:৫৬:৫৮ PM
bn-IN: ৩/৯/২০২১ ৫:৫৬:৫৮ PM
cs-CZ: 3. 9. 2021 17:56:58
da-DK: 3.9.2021 17.56.58
de-AT: 3.9.2021, 17:56:58
de-CH: 3.9.2021, 17:56:58
de-DE: 3.9.2021, 17:56:58
el-GR: 3/9/2021, 5:56:58 μ.μ.
en-AU: 03/09/2021, 5:56:58 pm
en-CA: 2021-09-03, 5:56:58 p.m.
en-GB: 03/09/2021, 17:56:58
en-IE: 3/9/2021, 17:56:58
en-IN: 3/9/2021, 5:56:58 pm
en-NZ: 3/09/2021, 5:56:58 pm
en-US: 9/3/2021, 5:56:58 PM
en-ZA: 2021/09/03, 17:56:58
es-AR: 3/9/2021 17:56:58
es-CL: 03-09-2021 17:56:58
es-CO: 3/9/2021, 5:56:58 p. m.
es-ES: 3/9/2021 17:56:58
es-MX: 3/9/2021 17:56:58
es-US: 3/9/2021 5:56:58 p. m.
fi-FI: 3.9.2021 klo 17.56.58
fr-BE: 03/09/2021, 17:56:58
fr-CA: 2021-09-03, 17 h 56 min 58 s
fr-CH: 03.09.2021, 17:56:58
fr-FR: 03/09/2021, 17:56:58
he-IL: 3.9.2021, 17:56:58
hi-IN: 3/9/2021, 5:56:58 pm
hu-HU: 2021. 09. 03. 17:56:58
id-ID: 3/9/2021 17.56.58
it-CH: 3/9/2021, 17:56:58
it-IT: 3/9/2021, 17:56:58
ja-JP: 2021/9/3 17:56:58
ko-KR: 2021. 9. 3. 오후 5:56:58
nl-BE: 3/9/2021 17:56:58
nl-NL: 3-9-2021 17:56:58
no-NO: 3.9.2021, 17:56:58
pl-PL: 3.09.2021, 17:56:58
pt-BR: 03/09/2021 17:56:58
pt-PT: 03/09/2021, 17:56:58
ro-RO: 03.09.2021, 17:56:58
ru-RU: 03.09.2021, 17:56:58
sk-SK: 3. 9. 2021, 17:56:58
sv-SE: 2021-09-03 17:56:58
ta-IN: 3/9/2021, பிற்பகல் 5:56:58
ta-LK: 3/9/2021, 17:56:58
th-TH: 3/9/2564 17:56:58
tr-TR: 03.09.2021 17:56:58
zh-CN: 2021/9/3 下午5:56:58
zh-HK: 3/9/2021 下午5:56:58
zh-TW: 2021/9/3 下午5:56:58
请注意,NodeJS 只会附带设备的语言环境格式,因此当您为 LocaleDateString 指定参数时,如下所示:
new Date("1983-March-25").toLocaleDateString('fr-CA', { year: 'numeric', month: '2-digit', day: '2-digit' })
'03/25/1983'
请注意,您期望"fr-CA"为您提供 YYYY-MM-DD,但它没有。那是因为它只使用美国区域设置,因为我的 Node 实例在美国区域设置中运行。
实际上,Node github帐户上有一个错误报告,描述了问题和解决方案:
https://github.com/nodejs/node/issues/8500
提供的解决方案是安装 full-icu
模块。
你可以尝试这样的事情:
var date = new Date(Date.UTC(2015, 0, 28, 4, 0, 0));
console.log(date.toLocaleDateString("nl",{year:"2-digit",month:"2-digit", day:"2-digit"}));
这至少在 Chrome (48.0.2564.116) 中给了我"28-01-15"。
Firefox 只返回"01/28/2015",而 phantomJS 返回"28/01/2015",而不考虑语言环境。
是的。它非常简单。您可以按如下方式使用 date 对象:
var d = new Date();
var mm = d.getMonth() + 1;
var dd = d.getDate();
var yy = d.getFullYear();
然后,您应该拥有以所需的任何格式形成字符串所需的数字。
var myDateString = yy + '-' + mm + '-' + dd; //(US)
请注意,如果数字为个位数,这将给出类似 2015-1-2 的内容,如果您需要 2015-01-02,则需要进一步转换。
另请注意,这只会给出"客户"日期,即用户系统上的日期。这应该是他们当地的时间。如果您需要服务器时间,那么您将不得不调用某种 api。
日期 : .toLocaleDateString('en-US', {day: "numeric"})
完整月份 : .toLocaleDateString('en-US', {month: "long"})
简而言之:.toLocaleDateString('en-US', {month: "short"})
全天 : .toLocaleDateString('en-US', {day: "long"})
简而言之:.toLocaleDateString('en-US', {day: "short"})
const options = { year: 'numeric', month: 'numeric', day: 'numeric' }
const new Date(date).toLocaleDateString('en', options)
显然 Date.prototype.toLocaleDateString() 在浏览器中不一致。您可以实现短日期格式的不同变体,如下所述:如何格式化 JavaScript Date 与浏览器文化有关?
var countries = [
"ar-SA",
"bn-BD",
"bn-IN",
"cs-CZ",
"da-DK",
"de-AT",
"de-CH",
"de-DE",
"el-GR",
"en-AU",
"en-CA",
"en-GB",
"en-IE",
"en-IN",
"en-NZ",
"en-US",
"en-ZA",
"es-AR",
"es-CL",
"es-CO",
"es-ES",
"es-MX",
"es-US",
"fi-FI",
"fr-BE",
"fr-CA",
"fr-CH",
"fr-FR",
"he-IL",
"hi-IN",
"hu-HU",
"id-ID",
"it-CH",
"it-IT",
"ja-JP",
"ko-KR",
"nl-BE",
"nl-NL",
"no-NO",
"pl-PL",
"pt-BR",
"pt-PT",
"ro-RO",
"ru-RU",
"sk-SK",
"sv-SE",
"ta-IN",
"ta-LK",
"th-TH",
"tr-TR",
"zh-CN",
"zh-HK",
"zh-TW"
];
countries.forEach( (country) => {
console.log( `${country}: ${new Date().toLocaleString(country)}` );
} );
对于此格式 YYYY-MM-DD,请使用此格式
var mm = ("0" + (date.getMonth() + 1)).slice(-2);
var dd =("0" + (date.getDate())).slice(-2);
var yy = date.getFullYear();
var dateString = yy + '-' + mm + '-' + dd;
结果示例
2023年08月21
- 使用.format在一些HTML代码中插入文本,会出现错误(Python)
- Format for Excel VBA addWatermarkFromText
- "responsed_to|format|“;引发UnknownFormat错误
- 如何将参数应用于String.prototype.format函数
- 如何使用Y.Number.format和decimalPlaces
- TypeError:format.match不是矩angularJS中的函数
- String.Format在TypeScript中不起作用
- YQL 返回 application/javascript,即使请求 format=json
- jQuery format date min & max date jQuery UI
- OpenLayers 3.13v: issue with ol.format.GeoJSON()
- Rails 4 ActionController::UnknownFormat format.js error
- toLocaleDateString() short format
- 使用string.format,最佳方法和替代方案在服务器端创建javascript函数
- 尝试使用 d3.time.format 格式化日期时,我收到 JavaScript 错误“d.getMonth 不是一个
- AngularJS JSON format
- Rails Edge Guides,AJAX示例 - 为什么同时使用“format.js”和“format.json”
- 如何使 String.Format 与 Javascript 中的资源文件值结合使用
- jQuery Flot xaxis Time Format
- jQuery.Ajax Format Path
- Angularjs directive and string.format