Get current date in DD-Mon-YYY format in JavaScript/Jquery

后端 未结 15 1587
攒了一身酷
攒了一身酷 2020-12-01 03:06

I need to get the date format as \'DD-Mon-YYYY\' in javascript. I had asked a question, and it got marked duplicate to jQuery date formatting

But, the answers provi

相关标签:
15条回答
  • 2020-12-01 03:14
    //convert DateTime result in jquery mvc 5 using entity fremwork 
    
    const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    
    
    function DateAndTime(date) {
    
        var value = new Date
            (
            parseInt(date.replace(/(^.*\()|([+-].*$)/g, ''))
        ); 
        var dat = value.getDate() +
            "-" +
            monthNames[value.getMonth()] +
            "-" +
            value.getFullYear();
    
        var hours = value.getHours();
        var minutes = value.getMinutes();
        var ampm = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0' + minutes : minutes;
        var strTime = hours + ':' + minutes + ' ' + ampm;
        return { Date: dat, Time: strTime };
    }
    // var getdate = DateAndTime(StartDate);
    //var Date = getdate.Date;//here get date
    //var time = getdate.Time;//here get Time
    //alert(Date)
    
    0 讨论(0)
  • 2020-12-01 03:16
    /*
      #No parameters
      returns a date with this format DD-MM-YYYY
    */
    function now()
    {
      var d = new Date();
      var month = d.getMonth()+1;
      var day = d.getDate();
    
      var output = (day<10 ? '0' : '') + day + "-" 
                  + (month<10 ? '0' : '') + month + '-'
                  + d.getFullYear();
    
      return output;
    }
    
    0 讨论(0)
  • 2020-12-01 03:17

    You can use toLocaleDateString and hunt for a format that's close to DD-mmm-YYYY (hint: 'en-GB'; you just need to replace the spaces with '-').

    const date = new Date();
    const formattedDate = date.toLocaleDateString('en-GB', {
      day: 'numeric', month: 'short', year: 'numeric'
    }).replace(/ /g, '-');
    console.log(formattedDate);

    0 讨论(0)
  • 2020-12-01 03:17

    I've made a custom date string format function, you can use that.

    var  getDateString = function(date, format) {
            var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            getPaddedComp = function(comp) {
                return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
            },
            formattedDate = format,
            o = {
                "y+": date.getFullYear(), // year
                "M+": months[date.getMonth()], //month
                "d+": getPaddedComp(date.getDate()), //day
                "h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
                 "H+": getPaddedComp(date.getHours()), //hour
                "m+": getPaddedComp(date.getMinutes()), //minute
                "s+": getPaddedComp(date.getSeconds()), //second
                "S+": getPaddedComp(date.getMilliseconds()), //millisecond,
                "b+": (date.getHours() >= 12) ? 'PM' : 'AM'
            };
    
            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    formattedDate = formattedDate.replace(RegExp.$1, o[k]);
                }
            }
            return formattedDate;
        };
    

    And now suppose you've :-

        var date = "2014-07-12 10:54:11";
    

    So to format this date you write:-

    var formattedDate = getDateString(new Date(date), "d-M-y")
    
    0 讨论(0)
  • 2020-12-01 03:19

    Use date format dd-MM-yy . It will output like: 16-December-2014.

    0 讨论(0)
  • 2020-12-01 03:22

    Here's a simple solution, using TypeScript:

      convertDateStringToDate(dateStr) {
        //  Convert a string like '2020-10-04T00:00:00' into '4/Oct/2020'
        let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
        let date = new Date(dateStr);
        let str = date.getDate()
                    + '/' + months[date.getMonth()]
                    + '/' + date.getFullYear()
        return str;
      }
    

    (Yeah, I know the question was about JavaScript, but I'm sure I won't be the only Angular developer coming across this article !)

    0 讨论(0)
提交回复
热议问题