在JavaScript / Jquery中以DD-Mon-YYY格式获取当前日期

我需要在javascript中将日期格式设为’DD-Mon-YYYY’。 我问了一个问题 ,它被标记为重复jQuery日期格式

但是,问题中提供的答案是以“DD-MM-YYYY”格式获取当前日期而不是“DD-MON-YYYY”。 其次,我没有使用datepicker插件。

你能帮助我,好像如何以“DD-Mon-YYYY”格式获取当前日期。

DD-Mon-YYYY javascript中没有原生格式。

你必须手动将它们全部放在一起。

答案的灵感来自: 如何格式化JavaScript日期

 // Attaching a new function toShortFormat() to any instance of Date() class Date.prototype.toShortFormat = function() { var month_names =["Jan","Feb","Mar", "Apr","May","Jun", "Jul","Aug","Sep", "Oct","Nov","Dec"]; var day = this.getDate(); var month_index = this.getMonth(); var year = this.getFullYear(); return "" + day + "-" + month_names[month_index] + "-" + year; } // Now any Date object can be declared var today = new Date(); // and it can represent itself in the custom format defined above. console.log(today.toShortFormat()); // 10-Jun-2018 

使用Moment.js库http://momentjs.com/它将为您省去很多麻烦。

 moment().format('DD-MMM-YYYY'); 

您可以使用toLocaleDateString并搜索接近DD-mmm-YYYY的格式(提示:’en-GB’;您只需要用’ – ‘替换空格)。

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

我已经制作了自定义日期字符串格式函数,你可以使用它。

 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; }; 

现在假设你已经: –

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

所以要格式化这个日期你写: –

 var formattedDate = getDateString(new Date(date), "dMy") 

DD-MM-YYYY只是其中一种格式。 jquery插件的格式基于以下列表: http : //docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

在chrome控制台中测试了以下代码:

 test = new Date() test.format('dM-Y') "15-Dec-2014" 
 /* #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; } 
 const date = new Date(); date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' })) 
 var date = new Date(); console.log(date.toJSON().slice(0,10).replace(new RegExp("-", 'g'),"/" ).split("/").reverse().join("/")+" "+date.toJSON().slice(11,19)); 

//输出:01/09/2016 18:30:00

  var today = new Date(); var formattedtoday = today.getDate() + '-' + (today.getMonth() + 1) + '-' + today.getFullYear(); alert(formattedtoday); 

使用日期格式dd-MM-yy 。 它将输出如:2014年12月16日。