Javascript 如何在jquery中获取当前日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8398897/
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 to get current date in jquery?
提问by Sara
I want to know how to use the Date() function in jQuery to get the current date in a yyyy/mm/dd
format.
我想知道如何使用 jQuery 中的 Date() 函数以某种yyyy/mm/dd
格式获取当前日期。
回答by Tadeck
Date()
is not part of jQuery
, it is one of JavaScript's features.
Date()
不是 的一部分jQuery
,它是 JavaScript 的特性之一。
See the documentation on Date object.
请参阅有关 Date 对象的文档。
You can do it like that:
你可以这样做:
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var output = d.getFullYear() + '/' +
(month<10 ? '0' : '') + month + '/' +
(day<10 ? '0' : '') + day;
See this jsfiddlefor a proof.
请参阅此 jsfiddle以获取证明。
The code may look like a complex one, because it must deal with months & days being represented by numbers less than 10
(meaning the strings will have one char instead of two). See this jsfiddlefor comparison.
代码可能看起来很复杂,因为它必须处理由小于的数字表示的月份和天数10
(意味着字符串将有一个字符而不是两个字符)。请参阅此 jsfiddle进行比较。
回答by Sara
If you have jQuery UI (needed for the datepicker), this would do the trick:
如果您有 jQuery UI(日期选择器需要),这可以解决问题:
$.datepicker.formatDate('yy/mm/dd', new Date());
回答by Connell
jQuery is JavaScript. Use the Javascript Date
Object.
jQuery 是 JavaScript。使用 JavascriptDate
对象。
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
回答by Pierre
Using pure Javascript your can prototype your own YYYYMMDD format;
使用纯 Javascript 您可以原型化您自己的 YYYYMMDD 格式;
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return yyyy + "/" + (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]); // padding
};
var date = new Date();
console.log( date.yyyymmdd() ); // Assuming you have an open console
回答by Ghostman
In JavaScript you can get the current date and time using the Date object;
在 JavaScript 中,您可以使用 Date 对象获取当前日期和时间;
var now = new Date();
This will get the local client machine time
这将获得本地客户端机器时间
Example for jquery LINK
jquery LINK示例
If you are using jQuery DatePicker you can apply it on any textfield like this:
如果您使用 jQuery DatePicker,您可以将它应用到任何文本字段,如下所示:
$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());
回答by fardjad
回答by Usman Younas
function GetTodayDate() {
var tdate = new Date();
var dd = tdate.getDate(); //yields day
var MM = tdate.getMonth(); //yields month
var yyyy = tdate.getFullYear(); //yields year
var currentDate= dd + "-" +( MM+1) + "-" + yyyy;
return currentDate;
}
Very handy function to use it , Enjoy
使用起来很方便的功能,享受
回答by No_Nick777
回答by Parth Jasani
Here is method top get current Day, Year or Month
这是 top 获取当前日、年或月的方法
new Date().getDate() // Get the day as a number (1-31)
new Date().getDay() // Get the weekday as a number (0-6)
new Date().getFullYear() // Get the four digit year (yyyy)
new Date().getHours() // Get the hour (0-23)
new Date().getMilliseconds() // Get the milliseconds (0-999)
new Date().getMinutes() // Get the minutes (0-59)
new Date().getMonth() // Get the month (0-11)
new Date().getSeconds() // Get the seconds (0-59)
new Date().getTime() // Get the time (milliseconds since January 1, 1970)