Javascript 如何在 jQuery datetimepicker 中格式化日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4802190/
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
How do I format date in jQuery datetimepicker?
提问by Upvote
I use jQuery datetimepicker
which was extended from jQuery datepicker
to pick not only date but time too.
我使用datetimepicker
从 jQuery 扩展而来的 jQuery datepicker
,不仅可以选择日期,还可以选择时间。
I want to set date/time format this way: dd-mm-yyyy @ hh:mm
我想以这种方式设置日期/时间格式: dd-mm-yyyy @ hh:mm
$('#timePicker').datetimepicker({
dateFormat: 'dd:mm:yyyy',
separator: ' @ ',
minDate: new Date()
});
But this does not work. I get date/time in following format:
但这不起作用。我以以下格式获取日期/时间:
Thu Jan 27 2011 02:05:17 GMT+0100
Is there any javascript function to format this date/time? If not how do I do that using the plugin? Check out my code: FIDDLE
是否有任何 javascript 函数来格式化此日期/时间?如果不是,我该如何使用插件来做到这一点?查看我的代码:FIDDLE
回答by Skilldrick
给你。
$('#timePicker').datetimepicker({
// dateFormat: 'dd-mm-yy',
format:'DD/MM/YYYY HH:mm:ss',
minDate: getFormattedDate(new Date())
});
function getFormattedDate(date) {
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear().toString().slice(2);
return day + '-' + month + '-' + year;
}
You need to pass datepicker() the date formatted correctly.
您需要传递 datepicker() 格式正确的日期。
回答by Indrit Kello
This works for me. Since it "extends" datepicker we can still use dateFormat:'dd/mm/yy'.
这对我有用。由于它“扩展”了日期选择器,我们仍然可以使用 dateFormat:'dd/mm/yy'。
$(function() {
$('.jqueryui-marker-datepicker').datetimepicker({
showSecond: true,
dateFormat: 'dd/mm/yy',
timeFormat: 'hh:mm:ss',
stepHour: 2,
stepMinute: 10,
stepSecond: 10
});
});
回答by Nate Bosscher
Newer versions of datetimepicker (I'm using is use 2.3.7) use format:"Y/m/d"
not dateFormat...
较新版本的 datetimepicker(我使用的是使用 2.3.7)format:"Y/m/d"
不使用dateFormat...
so
所以
jQuery('#timePicker').datetimepicker({
format: 'd-m-y',
value: new Date()
});
回答by ShirleyCC
For example, I get date/time in format Spanish.
例如,我以西班牙语格式获取日期/时间。
$('#timePicker').datetimepicker({
defaultDate: new Date(),
format:'DD/MM/YYYY HH:mm'
});
回答by sandeep kumar
you can use :
您可以使用 :
$('#timePicker').datetimepicker({
format:'d.m.Y H:i',
minDate: ge_today_date(new Date())
});
function ge_today_date(date) {
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear().toString().slice(2);
return day + '-' + month + '-' + year;
}
回答by Kunal Tyagi
You can use the format
option, as described in the documentation
$("#timePicker").datetimepicker({
format: 'Y-m-d H:i'
});
回答by Tayyab Hayat
this worked for me.
这对我有用。
$(document).ready(function () {
$("#datePicker").datetimepicker({
format: 'DD/MM/YYYY HH:mm:ss',
defaultDate: new Date(),
});
}
here are the CDN links
这是 CDN 链接
<!-- datetime picker -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.1.4/css/bootstrap-datetimepicker.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/3.1.4/js/bootstrap-datetimepicker.min.js"></script>