Ich muss das Datumsformat als 'DD-Mo-JJJJ' in Javascript erhalten Ich hatte eine Frage gestellt, und es wurde ein Duplikat in jQuery-Datumsformatierung
Die Antworten in der Frage lauten jedoch, das aktuelle Datum im Format "DD-MM-JJJJ" und nicht "DD-MON-JJJJ" .. zu erhalten. Zweitens verwende ich kein Datepicker-Plugin.
Können Sie mir bitte helfen, als ob Sie das aktuelle Datum im Format "DD-Mo-JJJJ" erhalten möchten.
Es gibt kein natives Format in Javascript für DD-Mon-YYYY
.
Sie müssen alles manuell zusammenstellen.
Die Antwort ist inspiriert aus: So formatieren Sie ein JavaScript-Datum
// 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
Verwenden Sie die Bibliothek von Moment.js http://momentjs.com/ Sie sparen eine Menge Ärger.
moment().format('DD-MMM-YYYY');
Sie können toLocaleDateString verwenden und nach einem Format suchen, das DD-mmm-YYYY nahe kommt (Hinweis: 'en-GB'; Sie müssen nur die Leerzeichen durch '-' ersetzen).
const date = new Date();
const formattedDate = date.toLocaleDateString('en-GB', {
day: 'numeric', month: 'short', year: 'numeric'
}).replace(/ /g, '-');
console.log(formattedDate);
Ich habe eine benutzerdefinierte Formatierungsfunktion für Datumszeichen erstellt, die Sie verwenden können.
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;
};
Und jetzt nehmen Sie an, Sie haben: -
var date = "2014-07-12 10:54:11";
Um dieses Datum zu formatieren, schreiben Sie: -
var formattedDate = getDateString(new Date(date), "d-M-y")
Daten übergeben changeFormate (15/07/2020)
changeFormate(date) {
let month_names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
let incomingDateChnge: any = new Date(date);
let incomingDay = incomingDateChnge.getDate();
let incomingMonth = incomingDateChnge.getMonth();
let incomingYear = incomingDateChnge.getFullYear();
if (incomingDay < 10) {
incomingDay = '0' + incomingDay;
}
incomingDateChnge = incomingDay + ' ' + month_names[incomingMonth] + ' ' + incomingYear;
return incomingDateChnge;
}
das DD-MM-JJJJ ist nur eines der Formate. Das Format des Jquery-Plugins basiert auf dieser Liste: http://docs.Oracle.com/javase/7/docs/api/Java/text/SimpleDateFormat.html
Folgender Code in der Chrome-Konsole getestet:
test = new Date()
test.format('d-M-Y')
"15-Dec-2014"
const date = new Date();
date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' }))
//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)
/*
#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;
}