//日期加减计算 function dateadd(sdate, delta, ymdh){ if(!sdate ) return; if(typeof sdate == 'object') sdate = sdate.toLocaleString(); /(\d{4})[\D](\d{1,2})[\D](\d{1,2})[\D]?\s(\d{1,2}):(\d{1,2}):(\d{1,2})/.exec(sdate); var a = [0,0,0,0]; switch(ymdh){ case 'y': a = [delta, -1, 0, 0]; break; case 'm': a=[0, delta-1, 0, 0]; break; case 'H': a=[0, -1, 0, delta]; break; default: a = [0, -1, delta, 0]; break; } println('date:' + (parseInt(RegExp.$1)+ a[0]) + '-'+ (parseInt(RegExp.$2)+a[1]) +'-' + (parseInt(RegExp.$3)+a[2]) + ' ' + (parseInt(RegExp.$4)+a[3]) +':' + RegExp.$5 + ':' +RegExp.$6); return new Date(parseInt(RegExp.$1)+ a[0], parseInt(RegExp.$2)+a[1], parseInt(RegExp.$3)+a[2], parseInt(RegExp.$4)+a[3], RegExp.$5,RegExp.$6); } //UTC转换 println('---------------------------------------------'); var sdate='2013-01-01 00:00:00.0'; var d = dateadd(sdate,0); var d2 = Date.UTC(d.getUTCFullYear(),d.getUTCMonth() ,d.getUTCDate(),d.getUTCHours(),d.getUTCMinutes(),d.getUTCSeconds()); println('原日期:' + sdate); println('d2:' + d2); println('d3:' + new Date(d2)); println('d4:' + new Date(d2).toLocaleString()); println('d5:' + d2.toLocaleString());
测试结果:
--------------------------------------------- date:2013-0-1 0:00:00 原日期:2013-01-01 00:00:00.0 d2:1356969600000 d3:Tue Jan 1 00:00:00 UTC+0800 2013 d4:2013年1月1日 0:00:00 d5:1,356,969,600,000.00