我想知道如何在jQuery中使用Date()函数以yyyy/mm/dd格式获取当前日期。


当前回答

var today = new Date(); var tday = getfullyear () + '/' + ( today.getMonth() < 9 ?'0': " ) + (today.getMonth() + 1) + '/' + ( getdate () < 10 ?'0': " ) + today.getDate(); console.log (tday);

其他回答

我只是想分享一个我用皮埃尔的想法做的时间戳原型。没有足够的点来评论:(

// US common date timestamp
Date.prototype.timestamp = function() {
  var yyyy = this.getFullYear().toString();
  var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
  var dd  = this.getDate().toString();
  var h = this.getHours().toString();
  var m = this.getMinutes().toString();
  var s = this.getSeconds().toString();

  return (mm[1]?mm:"0"+mm[0]) + "/" + (dd[1]?dd:"0"+dd[0]) + "/" + yyyy + " - " + ((h > 12) ? h-12 : h) + ":" + m + ":" + s;
};

d = new Date();

var timestamp = d.timestamp();
// 10/12/2013 - 2:04:19

jQuery就是JavaScript。使用Javascript日期对象。

var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
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;
}

非常方便的功能使用,享受。你不需要任何javascript框架。它只是在简单的javascript工作。

下面是获取当前日、年或月的方法

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)

试试这个…

var d = new Date();
alert(d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate());

getMonth()返回月份0到11,因此我们希望添加1来表示准确的月份

参考:https://www.w3schools.com/jsref/jsref_obj_date.asp