locked
how to get current date in format RRS feed

  • Question

  • User450763225 posted

    Hi, I am trying to format the date like the below,

    8/8/2016--> 08 August 2016

    Monday, August 8, 2016 1:12 PM

Answers

  • User2103319870 posted

    Hi, I am trying to format the date like the below,

    8/8/2016--> 08 August 2016

    You can use the below code if you want to use pure javascript approach

    var monthNames = [
      "January", "February", "March",
      "April", "May", "June", "July",
      "August", "September", "October",
      "November", "December"
    ];
    
    //Convert variable to date value
    var d = new Date("8/8/2016");
    //Get the current month index
    var monthIndex = d.getMonth();
    
    
    //Set the format of Date to MM/dd/YYYY
    var newDate =  ('0' + d.getDate()).slice(-2) +' '+monthNames[monthIndex]+' '+d.getFullYear();
    document.write(newDate);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 1:43 PM
  • User2103319870 posted

    Additionally I would highly recommend using use a library like Moment.js which when used would allow you to easily specify specific formatting strings like you do in C#

    Sample

     var myDate = "8/8/2016"; 
        var formattedDate = moment(new Date(myDate)).format('DD MMMM YYYY');

    Complete  Code

    <!DOCTYPE html>
    <html>
    <head>
    <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src=" https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
    <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
    <script>
      $(document).ready(function() {
        var myDate = "8/8/2016"; 
        var formattedDate = moment(new Date(myDate)).format('DD MMMM YYYY');
        
    alert("DD MMMM yyyy : " + formattedDate);
        
      });
    </script>
    </head>
    <body>
    
    </body>
    </html>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 2:05 PM

All replies

  • User2103319870 posted

    Hi, I am trying to format the date like the below,

    8/8/2016--> 08 August 2016

    You can use the below code if you want to use pure javascript approach

    var monthNames = [
      "January", "February", "March",
      "April", "May", "June", "July",
      "August", "September", "October",
      "November", "December"
    ];
    
    //Convert variable to date value
    var d = new Date("8/8/2016");
    //Get the current month index
    var monthIndex = d.getMonth();
    
    
    //Set the format of Date to MM/dd/YYYY
    var newDate =  ('0' + d.getDate()).slice(-2) +' '+monthNames[monthIndex]+' '+d.getFullYear();
    document.write(newDate);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 1:43 PM
  • User2103319870 posted

    Additionally I would highly recommend using use a library like Moment.js which when used would allow you to easily specify specific formatting strings like you do in C#

    Sample

     var myDate = "8/8/2016"; 
        var formattedDate = moment(new Date(myDate)).format('DD MMMM YYYY');

    Complete  Code

    <!DOCTYPE html>
    <html>
    <head>
    <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src=" https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
    <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
    <script>
      $(document).ready(function() {
        var myDate = "8/8/2016"; 
        var formattedDate = moment(new Date(myDate)).format('DD MMMM YYYY');
        
    alert("DD MMMM yyyy : " + formattedDate);
        
      });
    </script>
    </head>
    <body>
    
    </body>
    </html>
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 8, 2016 2:05 PM