javascript Jquery 新日期 - 转换为 yyyy-MM-dd 并使用 toLocaleDateString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33204168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery new Date - Convert to yyyy-MM-dd and use toLocaleDateString
提问by Saj
I have a HTML input box set to type=date
我有一个 HTML 输入框设置为 type=date
<input name="datereceived" type="date" class="FormDateValues" id="datereceived" />
I want to use Jquery to fill this input box on document load to today's date but because I am in Australia, I have to offset using GMT+10
我想在文档加载到今天的日期时使用 Jquery 填充此输入框,但因为我在澳大利亚,我必须使用 GMT+10 进行偏移
To get the date, I do the following:
为了获得日期,我执行以下操作:
var newDate = new Date();
and then I tried to set the input box using the following:
然后我尝试使用以下方法设置输入框:
$('#datereceived').val(newDate.toLocaleDateString());
The browser tells me that for type=date, the format must be set to yyyy-MM-dd and not the Australian time format of dd/MM/yyyy.
浏览器告诉我,对于 type=date,格式必须设置为 yyyy-MM-dd 而不是 dd/MM/yyyy 的澳大利亚时间格式。
I am not sure how to go about enforcing the toLocaleDateString AND converting the date to yyyy-MM-dd.
我不确定如何执行 toLocaleDateString 并将日期转换为 yyyy-MM-dd。
采纳答案by guest271314
Try using String.prototype.split()
, Array.prototype.splice()
, Array.prototype.join()
尝试使用String.prototype.split()
, Array.prototype.splice()
,Array.prototype.join()
// create array of values returned by `.toLocaleDateString()`,
// delimited by `/`
var d = new Date().toLocaleDateString().split("/");
// `y` : year
var y = d.splice(-1)[0];
// set `y` as item at index `0` of `d`
d.splice(0, 0, y);
// join items within `d` with dash character `"-"`
var date = d.join("-");
console.log(date);
回答by atiruz
Try using fr-CA
that returns the format in yyyy-MM-dd
.
尝试使用fr-CA
返回yyyy-MM-dd
.
var yyyymmdd = (new Date ()).toLocaleDateString ("fr-CA");
console.log (yyyymmdd);
回答by Leon Adler
If you need the local date (GMT+10 in your case), you need to use methods of Date
:
如果您需要本地日期(在您的情况下为 GMT+10),则需要使用以下方法Date
:
function toHtmlDate (d) {
return (d.getFullYear() + '-0' + (d.getMonth() + 1) + '-0' + d.getDate()).replace(/-0(\d\d)/g, '-');
}
If you need an ISO date (GMT), you can use new Date().toISOString().substring(0, 10)
. For explanation:
如果您需要 ISO 日期 (GMT),您可以使用new Date().toISOString().substring(0, 10)
. 解释:
Date.prototype.toLocaleDateString
output depends on your locale, using string splitting on it might not work for another userDate.prototype.toISOString
always returns####-##-##T##:##:##.###<timezone>
, for example2015-10-18T23:23:22.880Z
. Take the first 10 characters, you have<year>-<month>-<date>
.
Date.prototype.toLocaleDateString
输出取决于您的语言环境,对它使用字符串拆分可能不适用于其他用户Date.prototype.toISOString
####-##-##T##:##:##.###<timezone>
例如,总是返回2015-10-18T23:23:22.880Z
。取前 10 个字符,你有<year>-<month>-<date>
.
回答by Maksym Kozlenko
Apart from solution mentined:
除了提到的解决方案:
If you have jQuery UI loaded:
如果您加载了 jQuery UI:
// jQuery UI datepicker
$.datepicker.formatDate("yy-mm-dd", new Date())
If you have Moment.JS
如果你有 Moment.JS
// Moment JS
moment().format("YYYY-MM-DD")
If you need additional tools for parsing, formatting, comaping dates or converting them between time zones, I recommend to have a look at Moment.jslibrary.
如果您需要其他工具来解析、格式化、合并日期或在时区之间转换它们,我建议您查看Moment.js库。