Skip to content
Loading
date format in jquery
 
Here i use Jquery and Moment js cdn links . In format function you can give your desire format.
you can learn more about moment js in their official website : https://momentjs.com 
+4
  • Ankit Kanojia
    Hi Reylin Mathew,
     
    Please try below single line code, it will work for you as per your requested format for date.
    1. new Date("8/9/2019").toLocaleDateString('en-US', {year: 'numeric', month: '2-digit', day: '2-digit'}); 
     This will return the date value with format like "08/09/2019"
    +2
  • Hello 
     
    Following code may help you:
     
    1.   
      function dateFormate(orginaldate) {
      var date = new Date(orginaldate);
      var day = date.getDate();
      var month = date.getMonth() + 1;
      var year = date.getFullYear();
      if (day < 10) {
      day = "0" + day;
      }
      if (month < 10) {
      month = "0" + month;
      }
      var date = month + "/" + day + "/" + year;
      return date;
      };
    +3
  • The JSON specification does not specify a format for exchanging dates which is why there are so many different ways to do it. The problem with dates in JSON and JavaScript in general – is that there's no equivalent literal representation for dates. In JavaScript/jQuery following Date constructor straight away converts the milliseconds since 1970 to Date as follows:
    var jsonDate = new Date(1297246301973);
    Then let's convert it to js format:
    var date = new Date(parseInt(jsonDate.substr(6)));
    The substr() function takes out the /Date( part, and the parseInt() function gets the integer and ignores the )/ at the end. The resulting number is passed into the Date constructor .
    For ISO-8601 formatted JSON dates, just pass the string into the Date constructor:
    var date = new Date(jsonDate);
     
    http://net-informations.com/jq/iq/jdate.htm
    +2
  • Ravishankar N
    Hi,
     
    Check out this
    1. var fullDate = new Date()  
    2.    
    3. //convert month to 2 digits  
    4. var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);  
    5.    
    6. var currentDate = fullDate.getDate() + "-" + twoDigitMonth + "-" + fullDate.getFullYear();  
    7. console.log(currentDate);  
    8. //08-09-2019  
     
    +4
  • Bidyasagar Mishra
    follow the below link 
    https://stackoverflow.com/questions/23946698/changing-date-time-format-using-jquery-javascript 
    +1
  • Vishal  Patel
    You can check below link to use moment js is best for any type of date conversion 
     
    please follow this url and generate output based on your requirment. 
     
    http://jsfiddle.net/JamesWClark/9PAFg/ 
    +1
  • Priyanka Jain
    You can check below link it uses date.format.js having format function which can format your date in which you want
     
    http://embed.plnkr.co/P05l1a/preview 
    +3
  • Amit Mohanty
    Try this
     
    1. function formatDate(date) {  
    2.      var d = new Date(date),  
    3.          month = '' + (d.getMonth() + 1),  
    4.          day = '' + d.getDate(),  
    5.          year = d.getFullYear();  
    6.   
    7.      if (month.length < 2) month = '0' + month;  
    8.      if (day.length < 2) day = '0' + day;  
    9.   
    10.      return [day, month, year].join('/');  
    11.  }  
    12.   
    13. document.write(formatDate('8/9/2019')); 
     
    +5