jQuery 如何使用jquery获取当前日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4710166/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:46:54  来源:igfitidea点击:

How to get current date using jquery?

jquerydatejquery-plugins

提问by Rinkal Bhanderi

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?

我正在使用日历插件并且我想将当前日期填充到文本字段中。我如何使用 jquery 做到这一点?

回答by Naveed

You can get current date in JavaScript:

您可以在 JavaScript 中获取当前日期

var now = new Date();

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

如果您使用 jQuery DatePicker,您可以将它应用到任何文本字段,如下所示:

$('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());

回答by Arun P Johny

You can find the sample here.

您可以在此处找到示例。

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