How to get current date using jquery?

北战南征 提交于 2019-12-23 09:17:36

问题


I am using a calender plug-in and I want to populate the current date into a text field. How can I do this using jquery?


回答1:


You can get current date in JavaScript:

var now = new Date();

If you are using jQuery DatePicker you can apply it on any textfield like this:

$('input.youTextFieldClass').datepicker({ dateFormat: 'mm/dd/yy', 
                                     changeMonth: true,
                                     changeYear: true,
                                     yearRange: '-70:+10',
                                     constrainInput: false,
                                     duration: '',
                                     gotoCurrent: true});

If you want to set current date in textfields as page load:

$("input.youTextFieldClass").datepicker('setDate', new Date());



回答2:


You can find the sample here.

<input type="text" id="datepicker">
$( "#datepicker" ).datepicker({dateFormat:"dd M yy"}).datepicker("setDate",new Date());


来源:https://stackoverflow.com/questions/4710166/how-to-get-current-date-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!