짧은 답변은 날짜 형식을 표준화하는 방법이 없으므로 ... 작성했습니다. 이 작은 라이브러리에는 몇 가지 제한 사항이 있습니다. 예를 들어, 나는 완전히 (내 모든 작품은 어쨌든 UTC입니다) 시간대를 무시하고 그리고 난 탈출 특수 문자를 확인하고 있지 않다, 그러나 그것은 내 목적을 위해 수행해야합니다
이
/************************************************************
Feel free to use this as you like.
************************************************************/
var shortMonths = [
'Jan',
'Feb',
'Mar',
'Apr',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
];
var fullMonths = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
var shortDays = [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat'
];
var fullDays = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
var shortAmPm = [
'A',
'P'
];
var fullAmPm = [
'AM',
'PM'
];
function dotNetDateTimeFormat(date, format) {
//we need a date
if (!(date instanceof Date && !isNaN(date.valueOf()))) return '';
//if no format is given, send back default yyyy-MM-dd
format = format || '';
if (format == '')
return d.getFullYear + '-' + padZeroes(d.getMonth(), 2) + '-' + padZeroes(d.getDate(), 2);
//check for standard formats
switch (format) {
case 'd': //short date pattern
return formatDateString(date, 'M/d/yyyy');
case 'D': //long date pattern
return formatDateString(date, 'dddd, MMMM d, yyyy');
case 'f': //Full date/time pattern (short time)
return formatDateString(date, 'dddd, MMMM d, yyyy h:mm tt');
case 'F': //Full date/time pattern (long time)
return formatDateString(date, 'dddd, MMMM d, yyyy h:mm:ss tt');
case 'g': //General date/time pattern (short time)
return formatDateString(date, 'M/d/yyyy h:mm tt');
case 'G': //General date/time pattern (long time)
return formatDateString(date, 'M/d/yyyy h:mm:ss tt');
case 'M': //Month/day pattern
case 'm': //Month/day pattern
return formatDateString(date, 'MMMM d');
case 'O': //Round-trip date/time pattern
case 'o': //Round-trip date/time pattern
return formatDateString(date, 'yyyy-MM-ddTHH:mm:ss.fff-00:00');
case 'R': //RFC1123 pattern
case 'r': //RFC1123 pattern
return formatDateString(date, 'ddd, d MMM yyyy HH:mm:ss GMT');
case 's': //Sortable date/time pattern
return formatDateString(date, 'yyyy-MM-ddTHH:mm:ss');
case 't': //Short time pattern
return formatDateString(date, 'h:mm tt');
case 'T': //Long time pattern
return formatDateString(date, 'h:mm:ss tt');
case 'u': //Universal sortable date/time pattern
return formatDateString(date, 'yyyy-MM-dd HH:mm:ssZ');
case 'U': //Universal full date/time pattern
return formatDateString(date, 'dddd, MMMM d, yyyy h:mm:ss tt');
case 'Y': //Year month pattern
case 'y': //Year month pattern
return formatDateString(date, 'MMMM, yyyy');
default: // custom string, no standard format.
return formatDateString(date, format);
}
}
function formatDateString(date, format) {
var yyyy = date.getFullYear();
var M = date.getMonth();
var d = date.getDate();
var day = date.getDay();
var H = date.getHours();
var h = H;
var pm = h > 12;
if (pm) h = H - 12;
var m = date.getMinutes();
var s = date.getSeconds();
var f = date.getMilliseconds();
format = format
.replace(/GMT/g, '*00*')
.replace(/yyyy/g, '*01*')
.replace(/yyy/g, '*02*')
.replace(/yy/g, '*03*')
.replace(/y/g, '*04*')
.replace(/MMMM/g, '*05*')
.replace(/MMM/g, '*06*')
.replace(/MM/g, '*07*')
.replace(/M/g, '*08*')
.replace(/dddd/g, '*09*')
.replace(/ddd/g, '*10*')
.replace(/dd/g, '*11*')
.replace(/d/g, '*12*')
.replace(/HH/g, '*13*')
.replace(/H/g, '*14*')
.replace(/hh/g, '*15*')
.replace(/h/g, '*16*')
.replace(/mm/g, '*17*')
.replace(/m/g, '*18*')
.replace(/ss/g, '*19*')
.replace(/s/g, '*20*')
.replace(/fff/g, '*21*')
.replace(/ff/g, '*22*')
.replace(/f/g, '*23*')
.replace(/FFF/g, '*24*')
.replace(/FF/g, '*25*')
.replace(/F/g, '*26*')
.replace(/tt/g, pm ? 'PM' : 'AM')
.replace(/t/g, pm ? 'P' : 'A')
.replace('*00*', 'GMT')
.replace('*01*', yyyy)
.replace('*02*', yyyy.toString().substr(-3, 3))
.replace('*03*', yyyy.toString().substr(-2, 2))
.replace('*04*', yyyy.toString().substr(-1, 1))
.replace('*05*', getFullMonth(M.toString()))
.replace('*06*', getShortMonth(M.toString()))
.replace('*07*', padZeroes(M.toString(), 2))
.replace('*08*', M.toString())
.replace('*09*', getFullDay(day.toString()))
.replace('*10*', getShortDay(day.toString()))
.replace('*11*', padZeroes(d.toString(), 2))
.replace('*12*', d.toString())
.replace('*13*', padZeroes(H.toString(), 2))
.replace('*14*', H.toString())
.replace('*15*', padZeroes(h.toString(), 2))
.replace('*16*', h.toString())
.replace('*17*', padZeroes(m.toString(), 2))
.replace('*18*', m.toString())
.replace('*19*', padZeroes(s.toString(), 2))
.replace('*20*', s)
.replace('*21*', padZeroes(f.toString(), 3))
.replace('*22*', padZeroes(Math.round(f/10), 2).toString())
.replace('*23*', Math.round(f/100).toString())
.replace('*24*', blankZero(padZeroes(f.toString(), 3)))
.replace('*25*', blankZero(padZeroes(Math.round(f/10), 2).toString()))
.replace('*26*', blankZero(Math.round(f/100).toString()))
;
return format;
}
function getShortMonth(month) {
return shortMonths[month];
}
function getFullMonth(month) {
return fullMonths[month];
}
function getShortDay(day) {
return shortDays[day];
}
function getFullDay(day) {
return fullDays[day];
}
function padZeroes(toPad, numDigits) {
toPad = toPad || '';
var zeroes = Array(numDigits).join('0');
return (zeroes + toPad).substr(-numDigits, numDigits);
}
function blankZero(number) {
var n = parseFloat(number);
if (isNaN(number)) return '';
if (n == 0.0) return '';
return n;
}
당신과 같이 사용할 수 있습니다 :
<div id="test-format"></div>
<script type="text/javascript">
var date = new Date(2009, 6, 15, 13, 45, 30, 660);
var formats = [
dotNetDateTimeFormat(date, 'd'),
dotNetDateTimeFormat(date, 'D'),
dotNetDateTimeFormat(date, 'f'),
dotNetDateTimeFormat(date, 'F'),
dotNetDateTimeFormat(date, 'g'),
dotNetDateTimeFormat(date, 'G'),
dotNetDateTimeFormat(date, 'm'),
dotNetDateTimeFormat(date, 'M'),
dotNetDateTimeFormat(date, 'o'),
dotNetDateTimeFormat(date, 'O'),
dotNetDateTimeFormat(date, 'r'),
dotNetDateTimeFormat(date, 'R'),
dotNetDateTimeFormat(date, 's'),
dotNetDateTimeFormat(date, 't'),
dotNetDateTimeFormat(date, 'T'),
dotNetDateTimeFormat(date, 'u'),
dotNetDateTimeFormat(date, 'U'),
dotNetDateTimeFormat(date, 'y'),
dotNetDateTimeFormat(date, 'Y'),
dotNetDateTimeFormat(date, 'yyyyMMddHHmmss')
];
$(function() {
var f = formats.join('<br />');
$('#test-format').html(f);
});
</script>
개선을 환영합니다.
Downvoter는 코멘트 하시겠습니까? –
이것은 SO를 주제로 한 주제입니다. 거의 7,000 명의 담당자와 함께 당신은 그것을 알아야합니다. –
...이 주제에 대해 어떻게 설명 할 수 있습니까? 이것은 .NET과 Javascript를 통합하는 것에 대한 명확한 설명입니다. –