Tad*_*eck 309
Date()
它不是jQuery
JavaScript的一部分,它是JavaScript的功能之一.
请参阅Date对象的文档.
你可以这样做:
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;
Run Code Online (Sandbox Code Playgroud)
请参阅此jsfiddle以获取证据.
代码可能看起来像一个复杂的代码,因为它必须处理由小于的数字表示的月和日10
(意味着字符串将有一个char而不是两个).请参阅此jsfiddle进行比较.
Sar*_*ara 124
如果你有jQuery UI(datepicker需要),这可以解决问题:
$.datepicker.formatDate('yy/mm/dd', new Date());
Run Code Online (Sandbox Code Playgroud)
Con*_*ell 43
jQuery是JavaScript.使用Javascript Date
对象.
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
Run Code Online (Sandbox Code Playgroud)
Pie*_*rre 30
使用纯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
Run Code Online (Sandbox Code Playgroud)
Gho*_*man 20
在JavaScript中,您可以使用Date对象获取当前日期和时间;
var now = new Date();
Run Code Online (Sandbox Code Playgroud)
这将获得本地客户端机器时间
jquery LINK的示例
如果您使用的是jQuery DatePicker,则可以在任何文本字段上应用它,如下所示:
$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());
Run Code Online (Sandbox Code Playgroud)
far*_*jad 15
由于问题标记为JQuery
:
如果您还在使用JQuery UI
,可以使用$.datepicker.formatDate()
:
$.datepicker.formatDate('yy/mm/dd', new Date());
Run Code Online (Sandbox Code Playgroud)
看这个演示.
Usm*_*nas 15
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;
}
Run Code Online (Sandbox Code Playgroud)
非常方便的功能,使用它,享受
小智 13
我知道我迟到了但这就是你所需要的
var date = (new Date()).toISOString().split('T')[0];
Run Code Online (Sandbox Code Playgroud)
toISOString() 使用 javascript 的内置函数。
var date = (new Date()).toISOString().split('T')[0];
Run Code Online (Sandbox Code Playgroud)
这是方法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)
Run Code Online (Sandbox Code Playgroud)
//convert month to 2 digits<p>
var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);
var currentDate = fullDate.getFullYear()+ "/" + twoDigitMonth + "/" + fullDate.getDate();
console.log(currentDate);<br>
//2011/05/19
Run Code Online (Sandbox Code Playgroud)
小智 6
您也可以使用 moment.js 来实现这一点。在您的 html 中包含 moment.js。
<script src="moment.js"></script>
Run Code Online (Sandbox Code Playgroud)
并在脚本文件中使用以下代码来获取格式化日期。
moment(new Date(),"YYYY-MM-DD").utcOffset(0, true).format();
Run Code Online (Sandbox Code Playgroud)
小智 5
当element只有一个符号时,此对象设置为零:
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
Run Code Online (Sandbox Code Playgroud)
此对象设置实际的全职,小时和日期:
function getActualFullDate() {
var d = new Date();
var day = addZero(d.getDate());
var month = addZero(d.getMonth()+1);
var year = addZero(d.getFullYear());
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
return day + ". " + month + ". " + year + " (" + h + ":" + m + ")";
}
function getActualHour() {
var d = new Date();
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
return h + ":" + m + ":" + s;
}
function getActualDate() {
var d = new Date();
var day = addZero(d.getDate());
var month = addZero(d.getMonth()+1);
var year = addZero(d.getFullYear());
return day + ". " + month + ". " + year;
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<span id='full'>a</span>
<br>
<span id='hour'>b</span>
<br>
<span id='date'>c</span>
Run Code Online (Sandbox Code Playgroud)
JQUERY VIEW:
$(document).ready(function(){
$("#full").html(getActualFullDate());
$("#hour").html(getActualHour());
$("#date").html(getActualDate());
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
719213 次 |
最近记录: |