I want to know how to use the Date() function in jQuery to get the current date in a yyyy/mm/dd format.
32 Answers
Date() is not part of jQuery, it is one of JavaScript's features.
See the documentation on Date object.
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 jsfiddle for a proof.
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 jsfiddle for comparison.
- 
                    2I didn't know that. I am searching to get current date in jquery.plz help. – Sara Dec 06 '11 at 11:14
 - 
                    Thanks for the answer :) Although I have never in my entire existence seen a date formatted like yyyy/mm/dd - is that used in some country? I've seen yyyy-mm-dd and yyyymmdd – Manachi Sep 27 '14 at 17:31
 - 
                    1@Manachi Yes It is use in Sri Lanka – Sara Dec 17 '14 at 17:09
 - 
                    2Also used in Palestine :) – Nada N. Hantouli Feb 04 '15 at 14:33
 - 
                    Also commonly used in Korea and South Africa – Muleskinner Nov 16 '15 at 20:43
 
If you have jQuery UI (needed for the datepicker), this would do the trick:
$.datepicker.formatDate('yy/mm/dd', new Date());
- 14,098
 - 13
 - 34
 - 50
 
- 
                    1Yes. I used jQuery UI, so this solution is perfect for me. Thanks. – Chen Li Yong Dec 22 '15 at 03:27
 
jQuery is JavaScript. Use the Javascript Date Object.
var d = new Date();
var strDate = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
- 13,925
 - 11
 - 59
 - 92
 
- 
                    3
 - 
                    2`getMonth()` returns numbers between `0` and `11`. This is quite common JavaScript mistake. Also `toString()` works in a different way than you described (see [this jsfiddle](http://jsfiddle.net/CVghR/) and [this documentation page](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toString)). To sum up: none of the solutions you have provided work properly. – Tadeck Dec 06 '11 at 11:25
 - 
                    1The month I have no excuse, mistake I've made before and haven't learned from it! `toString` though I swear worked, but I tested your jsFiddle with Chrome and you're right. Removed from my answer. Thanks. – Connell Dec 06 '11 at 11:41
 
Using pure Javascript your can prototype your own YYYYMMDD format;
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
In JavaScript you can get the current date and time using the Date object;
var now = new Date();
This will get the local client machine time
Example for jquery LINK
If you are using jQuery DatePicker you can apply it on any textfield like this:
$( "#datepicker" ).datepicker({dateFormat:"yy/mm/dd"}).datepicker("setDate",new Date());
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. You do not require any javascript framework. it just works in with plain javascript.
- 1,323
 - 15
 - 21
 
- 
                    2This worked beautifully to set the value and then a subsequent call to hide the date-picker fixed it up in every browser. Neat! – nicholeous Jun 20 '14 at 17:46
 
I know I am Late But This Is All You Need
var date = (new Date()).toISOString().split('T')[0];
toISOString() use built function of javascript.
cd = (new Date()).toISOString().split('T')[0];
console.log(cd);
alert(cd);
- 394
 - 3
 - 11
 
- 
                    2
 - 
                    1I don't know if there are any exceptional conditions but this works fine. thanks. – Ajowi Apr 25 '21 at 10:28
 
Here is method top get current Day, Year or Month
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)
- 2,349
 - 1
 - 19
 - 26
 
See this.
The $.now() method is a shorthand for the number returned by the expression (new Date).getTime().
- 19,327
 - 10
 - 37
 - 52
 
- 141
 - 1
 - 5
 
Moment.js makes it quite easy:
moment().format("YYYY/MM/DD")
- 19,327
 - 10
 - 37
 - 52
 
- 110,878
 - 29
 - 149
 - 111
 
this object set zero, when element has only one symbol:
function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}
This object set actual full time, hour and date:
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;
}
HTML:
<span id='full'>a</span>
<br>
<span id='hour'>b</span>
<br>    
<span id='date'>c</span>
JQUERY VIEW:
$(document).ready(function(){
    $("#full").html(getActualFullDate());
    $("#hour").html(getActualHour());
    $("#date").html(getActualDate());
});
You can achieve this with moment.js as well. Include moment.js in your html.
<script src="moment.js"></script>
And use below code in script file to get formatted date.
moment(new Date(),"YYYY-MM-DD").utcOffset(0, true).format();
- 71
 - 3
 - 8
 
//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
- 2,457
 - 1
 - 30
 - 50
 
FYI - getDay() will give you the day of the week... ie: if today is Thursday, it will return the number 4 (being the 4th day of the week).
To get a proper day of the month, use getDate().
My example below... (also a string padding function to give a leading 0 on single time elements. (eg: 10:4:34 => 10:04:35)
function strpad00(s)
{
    s = s + '';
    if (s.length === 1) s = '0'+s;
    return s;
}
var currentdate = new Date();
var datetime = currentdate.getDate() 
    + "/" + strpad00((currentdate.getMonth()+1)) 
    + "/" + currentdate.getFullYear() 
    + " @ " 
    + currentdate.getHours() + ":" 
    + strpad00(currentdate.getMinutes()) + ":" 
    + strpad00(currentdate.getSeconds());
Example output: 31/12/2013 @ 10:07:49
If using getDay(), the output would be 4/12/2013 @ 10:07:49
- 706
 - 1
 - 6
 - 22
 
- 148
 - 6
 
you can use this code:
var nowDate     = new Date();
var nowDay      = ((nowDate.getDate().toString().length) == 1) ? '0'+(nowDate.getDate()) : (nowDate.getDate());
var nowMonth    = ((nowDate.getMonth().toString().length) == 1) ? '0'+(nowDate.getMonth()+1) : (nowDate.getMonth()+1);
var nowYear     = nowDate.getFullYear();
var formatDate  = nowDay + "." + nowMonth + "." + nowYear;
you can find a working demo here
This will give you current date string
var today = new Date().toISOString().split('T')[0];
- 1,103
 - 1
 - 9
 - 13
 
Try this....
var d = new Date();
alert(d.getFullYear()+'/'+(d.getMonth()+1)+'/'+d.getDate());
getMonth() return month 0 to 11 so we would like to add 1 for accurate month
Reference by : https://www.w3schools.com/jsref/jsref_obj_date.asp
- 19,327
 - 10
 - 37
 - 52
 
- 4,355
 - 10
 - 42
 - 61
 
var d = new Date();
var today = d.getFullYear() + '/' + ('0'+(d.getMonth()+1)).slice(-2) + '/' + ('0'+d.getDate()).slice(-2);
- 31
 - 3
 
- 
                    Is now corrected. d.getMonth()+1 has to be calculated (= set between brackets) before adding '0' in front of the string... – Filip May 24 '16 at 09:40
 
Using the jQuery-ui datepicker, it has a handy date conversion routine built in so you can format dates:
var my_date_string = $.datepicker.formatDate( "yy-mm-dd",  new Date() );
Simple.
- 1,630
 - 19
 - 17
 
This is what I came up with using only jQuery. It's just a matter of putting the pieces together.
        //Gather date information from local system
        var ThisMonth = new Date().getMonth() + 1;
        var ThisDay = new Date().getDate();
        var ThisYear = new Date().getFullYear();
        var ThisDate = ThisMonth.toString() + "/" + ThisDay.toString() + "/" + ThisYear.toString();
        //Gather time information from local system
        var ThisHour = new Date().getHours();
        var ThisMinute = new Date().getMinutes();
        var ThisTime = ThisHour.toString() + ":" + ThisMinute.toString();
        //Concatenate date and time for date-time stamp
        var ThisDateTime = ThisDate  + " " + ThisTime;
- 21
 - 1
 
function createDate() {
            var date    = new Date(),
                yr      = date.getFullYear(),
                month   = date.getMonth()+1,
                day     = date.getDate(),
                todayDate = yr + '-' + month + '-' + day;
            console.log("Today date is :" + todayDate);
- 1,178
 - 8
 - 15
 
The jQuery plugin page is down. So manually:
function strpad00(s)
{
    s = s + '';
    if (s.length === 1) s = '0'+s;
    return s;
}
var now = new Date();
var currentDate = now.getFullYear()+ "/" + strpad00(now.getMonth()+1) + "/" + strpad00(now.getDate());
console.log(currentDate );
- 22,828
 - 17
 - 107
 - 180
 
You can do this:
    var now = new Date();
    dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
     // Saturday, June 9th, 2007, 5:46:21 PM
OR Something like
    var dateObj = new Date();
    var month = dateObj.getUTCMonth();
    var day = dateObj.getUTCDate();
    var year = dateObj.getUTCFullYear();
    var newdate = month + "/" + day + "/" + year;
    alert(newdate);
- 2,029
 - 28
 - 45
 
var d = new Date();
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getYear();
var today = (day<10?'0':'')+ day + '/' +(month<10?'0':'')+ month + '/' + year;
alert(today);
- 5,779
 - 3
 - 29
 - 43
 
- 21
 - 1
 
- 
                    This is basically a copy of the accepted answer except in the wrong format (d/m/y) – Rob Grzyb May 03 '13 at 16:49
 
I just wanted to share a timestamp prototype I made using Pierre's idea. Not enough points to comment :(
// 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
- 808
 - 1
 - 8
 - 20
 
Get current Date format dd/mm/yyyy
Here is the code:
var fullDate = new Date();
var twoDigitMonth = ((fullDate.getMonth().toString().length) == 1)? '0'+(fullDate.getMonth()+1) : (fullDate.getMonth()+1);
var twoDigitDate = ((fullDate.getDate().toString().length) == 1)? '0'+(fullDate.getDate()) : (fullDate.getDate());
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
alert(currentDate);
You can add an extension method to javascript.
Date.prototype.today = function () {
    return ((this.getDate() < 10) ? "0" : "") + this.getDate() + "/" + (((this.getMonth() + 1) < 10) ? "0" : "") + (this.getMonth() + 1) + "/" + this.getFullYear();
}
- 3,721
 - 11
 - 40
 - 82
 
- 2,162
 - 1
 - 20
 - 26
 
This one-liner will give you YYYY-MM-DD:
new Date().toISOString().substr(0, 10)
'2022-06-09'
- 1,238
 - 2
 - 17
 - 32
 
var today = new Date();
var tday= 
today.getFullYear() + '/' + (
today.getMonth() < 9 ? '0' : ''
) + (today.getMonth() + 1) + '/' + (
today.getDate() < 10 ? '0' : ''
) + today.getDate() ; 
console.log(tday);
- 
                    It's the solution of Connell https://stackoverflow.com/a/8398914/5391965, but a little more complex – Reynadan Jun 01 '22 at 12:56
 
function returnCurrentDate() {
                var twoDigitMonth = ((fullDate.getMonth().toString().length) == 1) ? '0' + (fullDate.getMonth() + 1) : (fullDate.getMonth() + 1);
                var twoDigitDate = ((fullDate.getDate().toString().length) == 1) ? '0' + (fullDate.getDate()) : (fullDate.getDate());
                var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
                return currentDate;
            }
- 1,894
 - 22
 - 21