从 jquery 更改日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19546855/
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
change date format from jquery
提问by GioBot
I have this date, that I get from jquery
我有这个日期,我从 jquery 得到的
Wed Oct 30 2013 09:05:17 GMT-0800 (Hora estándar Pacífico (México))
that I get this function
我得到这个功能
var date = new Date();
var newdate = new Date(date);
newdate.setDate(newdate.getDate() + 7);
var nd = new Date(newdate);
$('#vigencia_receta_11').val(nd);
But I only need the date not the time, I want to format the date like this "DD/MM/YYYY"
但我只需要日期而不是时间,我想将日期格式化为“DD/MM/YYYY”
采纳答案by DropAndTrap
I did like the following code:
我确实喜欢以下代码:
function myDateFormatter ("pass your date here") {
var d = new Date(dateObject);
var day = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
if (day < 10) {
day = "0" + day;
}
if (month < 10) {
month = "0" + month;
}
var date = day + "/" + month + "/" + year;
return date;
};
回答by Christian Ternus
A couple of options.
几个选项。
If you're OK with including jQueryUI: $("#vigencia_receta_11").val($.datepicker.formatDate('dd/M/yy', nd));
如果您同意包含 jQueryUI: $("#vigencia_receta_11").val($.datepicker.formatDate('dd/M/yy', nd));
Otherwise, the jQuery dateFormat plugindoes something similar: $("#vigencia_receta_11").val($.format.date(nd, 'dd/M/yy'));
否则,jQuery dateFormat 插件会做类似的事情:$("#vigencia_receta_11").val($.format.date(nd, 'dd/M/yy'));
回答by Nick
The date object has functions for getting access to the individual date components. You can use:
日期对象具有访问各个日期组件的功能。您可以使用:
$('#vigencia_receta_11').val((nd.getMonth() + 1) + "/" + nd.getDate() + "/" + nd.getFullYear());
Note that getMonth() returns a zero-indexed date, so you'll need to add 1 to get it to a human-readable date format.
请注意,getMonth() 返回一个索引为零的日期,因此您需要加 1 以使其成为人类可读的日期格式。