将.NET日期/时间格式字符串转换为Javascript日期/时间格式字符串 [英] Convert .NET date/ time format string to Javascript date/ time format string

查看:113
本文介绍了将.NET日期/时间格式字符串转换为Javascript日期/时间格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个javascript库或其他机制可以让我传递.NET日期/时间格式字符串(即 yyyy-MM-dd HH:mm:ss )到一个JavaScript函数,并相应地解析提供的日期时间值?我一直在寻找一段时间,但我似乎无法找到我正在寻找什么。



我想象中的用法将允许我提供自定义格式字符串从一个.NET提供程序,并允许我现有的JavaScript库(如剑道)持续呈现日期/时间。



由于似乎有一些关于我是什么的困惑问,我会尽量做得更详细:

我有一个用户首选项表格,允许我的用户选择它们的日期格式,时间戳和时间等等,并且它可以使用.NET字符串格式完全自定义。不过,我也在使用Kendo,它会从我的服务器上收集原始日期/时间数据。我需要在JavaScript库和.NET呈现引擎之间一致地呈现它所呈现的数据格式。

所以简短的回答似乎是,没有办法来标准化日期格式,所以...我写了一个。这个小库有几个限制;例如,我完全忽略时区(我的所有工作都是UTC),而且我没有检查转义的特殊字符,但它应该用于我的目的:

  / ********* ********************* 
随意使用这个,只要你喜欢。
********************************************** ************** /
var shortMonths = [
'Jan',
'Feb',
'Mar',
'Apr',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
' 11月',
'Dec'
];

var fullMonths = [
'January',
'February',
'March',
'April',
'May ',
'六月',
'七月',
'八月',
'九月',
'十月',
'十一月',
'12月'
];

var shortDays = [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu ',
'Fri',
'Sat'
];

var fullDays = [
'星期日',
'星期一',
'星期二',
'星期三',
'星期四',
'Friday',
'Saturday'
];

var shortAmPm = [
'A',
'P'
];

var fullAmPm = [
'AM',
'PM'
];

函数dotNetDateTimeFormat(日期格式){
//我们需要一个日期
如果(!(日期和日期instanceof Date&&!isNaN(date.valueOf()) ))return'';

//如果没有给出格式,则返回默认值yyyy-MM-dd
format = format || ;
if(format =='')
return d.getFullYear +' - '+ padZeroes(d.getMonth(),2)+' - '+ padZeroes(d.getDate(),2) ;

//检查标准格式
切换(格式){
案例'd'://短日期模式
返回formatDateString(日期,'M / d月/年');
case'D':// long date pattern
return formatDateString(date,'dddd,MMMM d,yyyy');
case'f'://完整日期/时间模式(短时间)
返回formatDateString(日期,'dddd,MMMM d,yyyy h:mm tt');
case'F'://完整日期/时间模式(长时间)
返回formatDateString(date,'dddd,MMMM d,yyyy h:mm:ss tt');
case'g'://常规日期/时间模式(短时间)
返回formatDateString(日期,'M / d / yyyy h:mm tt');
case'G'://常规日期/时间模式(长时间)
返回formatDateString(日期,'M / d / yyyy h:mm:ss tt');
case'M':// Month / day pattern
case'm':// Month / day pattern
return formatDateString(date,'MMMM d');
case'O'://往返日期/时间模式
大小写'o'://往返日期/时间模式
返回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'://可排序日期/时间模式
返回formatDateString(日期,'yyyy-MM-ddTHH:mm:ss');
case't'://短时间模式
返回formatDateString(date,'h:mm tt');
case'T'://长时间模式
返回formatDateString(date,'h:mm:ss tt');
case'u'://通用排序日期/时间模式
返回formatDateString(日期,'yyyy-MM-dd HH:mm:ssZ');
case'U'://通用完整日期/时间模式
返回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');
默认值://自定义字符串,无标准格式。
返回formatDateString(日期,格式);



函数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();
$ b格式=格式
.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,' * / * * * *)
.replace(/ M / g,'* 08 *')
.replace(/ dddd / g,'* 09 *')
.replace(/ ddd / g,'* 10 *')
.replace(/ dd / g,'* 11 *')
.replace(/ d / g,'* 12 *')
。替换(/ 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()))
;

返回格式;
}

函数getShortMonth(月){
return shortMonths [month];
}

function getFullMonth(month){
return fullMonths [month];
}

function getShortDay(day){
return shortDays [day];
}

function getFullDay(day){
return fullDays [day];
}

函数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;

$ / code>

您可以像这样使用它:

 < 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'),$ b $ dotNetDateTimeFormat(date,'D'),
dotNetDateTimeFormat(date,'f'),
dotNetDateTimeFormat(date,'F'),
dotNetDateTimeFormat(date,'g'),
dotNetDateTimeFormat(date,'G'),
dotNetDateTimeFormat(date,'m'),$ b $ 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'),$ b $ dotnetDateTimeFormat(date, 'Y'),
dotNetDateTi meFormat(日期,'yyyyMMddHHmmss')
];

$(function(){
var f = formats.join('< br />');
$('#test-format')。html (f);
});

< / script>

欢迎大家改进。


Is there a javascript library or some other mechanism that will allow me to pass a .NET date/ time format string (i.e., yyyy-MM-dd HH:mm:ss) to a javascript function and have it parse a supplied datetime value accordingly? I've been looking for a while but I can't seem to find exactly what I am looking for.

My imagined usage will allow me to provide a custom format string from a .NET provider, and allow my existing javascript libraries (like Kendo) to render the date/time consistently.

Since there seem to be some confusion about what I am asking, I will try to be more detailed:

I have a User Preferences table that allows my users to choose the format of their dates, their timestamps, and their times, among other things, and it is completely customizable using .NET string formatting. Rendering these is easy from my .NET application.

However, I am also using Kendo, and it will collect raw date/time data from my server; I need to have the data it renders formatted consistently between the javascript library and the .NET rendering engine.

解决方案

So the short answer seems to be, there is not a means to standardize date formats, so... I wrote one. There are several limitations with this little library; for example, I'm completely ignoring time zones (all my work is UTC anyway) and I am not checking for escaped special characters, but it should do for my purposes:

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

You can use it like so:

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

Improvements are welcome.

这篇关于将.NET日期/时间格式字符串转换为Javascript日期/时间格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆