JSの日付と日付を書式設定する文字列の日付変換


jsは日付オブジェクトを文字列にフォーマットします.
//完全なフォーマットjs方法 var time 2=new Date().format(「yyy-M-dd HH:mm:ss」)//czhiバックグランドのフォーマット方法now.Date.ToString("yy-M-dd HH:mm:ss")
//        var time2 = new Date().format("yyyy-MM-dd HH:mm:ss");
Date.prototype.format = function (fmt) {
	var o = {
		"M+": this.getMonth() + 1, //  
		"d+": this.getDate(), // 
		"h+": this.getHours(), //  
		"m+": this.getMinutes(), // 
		"s+": this.getSeconds(), // 
		"q+": Math.floor((this.getMonth() + 3) / 3), //  
		"S": this.getMilliseconds() //  
	};
	//    
	var reYear = /(y+)/;
	var resultYear = reYear.exec(fmt);
	if (resultYear) {
		var yearformatPart = resultYear[0]; //         
		var yearVal = (this.getFullYear() + "").substr(4 - yearformatPart.length);
		fmt = fmt.replace(yearformatPart, yearVal);
	}
	for (var k in o) {
		var re = new RegExp("(" + k + ")");
		var res = re.exec(fmt);
		if (res) {
			var Val = "" + o[k]; //         
			var formatPart = res[0]; //         
			var replaceVal = (formatPart.length == 1) ? (Val) : (("00" + Val).substr(Val.length));
			fmt = fmt.replace(formatPart, replaceVal);
		}
	}
	return fmt;
}
Date.prototype.AddDays = function (days) {
	var olddays = this.getDate();
	var newdays = olddays + days;
	this.setDate(newdays);
	return this;
}
上に書式設定された文字列を日付オブジェクトに変換します.
/*      ,   ,     ,      
               ,       
    
var data=new Date();
var  format ="yyyy-MM-dd HH:mm:ss";
var text=data.format(format);
alert(text);
var data2=parserToDate(text,format);
alert(data2.format(format));
 */
function parserToDate(dateText, format) {
	if (!dateText) { //  
		return new Date();
	}	
	//     ,           
	var defaultVal = new Date();
	var year = defaultVal.getFullYear();
	var month = defaultVal.getMonth() + 1;
	var day = defaultVal.getDate();
	var hours = defaultVal.getHours();
	var minutes = defaultVal.getMinutes();
	var seconds = defaultVal.getSeconds();
	var milliseconds = defaultVal.getMilliseconds();
	//    
	var o = [{
			reg: "y+"
		}, //  
		{
			reg: "M+"
		}, //  
		{
			reg: "d+"
		}, // 
		{
			reg: "H+"
		}, //  
		{
			reg: "m+"
		}, // 
		{
			reg: "s+"
		}, // 
		{
			reg: "S"
		} //  
	];
	//      ,   ,               
	var index = 1;
	for (var j = 0; j < format.length; j++) {
		var onechar = format[j];
		for (var i = 0; i < o.length; i++) {
			var onepart = o[i]
				var onepartReg = onepart.reg;
			if (onepartReg[0] == onechar) {
				if (!onepart.index) {
					onepart.index = index; //      ,   ,               
					index++;
				}
				break;
			}
		}

	}
	//          
	for (var i = 0; i < o.length; i++) {
		var onepart = o[i]
			var onepartReg = onepart.reg;
		var re = new RegExp("(" + onepartReg + ")");
		var res = re.exec(format);
		if (res) {
			var formatPart = res[0]; //         
			var replaceVal = "([0-9]{" + formatPart.length + "})"; //    /d  ,     d  
			format = format.replace(formatPart, replaceVal);
		}
	}
	//             
	var dataInfo = {};
	var re = new RegExp(format);
	var res = re.exec(dateText);
	//       ,        
	if (res) {
		for (var i = 1; i < res.length; i++) {
			dataInfo[i] = res[i]; //     ,   ,                  
		}
	} else {
		alert(dateText + "     " + oldformat);
	}
	if (o[0].index) //    
	{
		var index = o[0].index;
		year = dataInfo[index];
	}
	if (o[1].index) //    
	{
		var index = o[1].index;
		month = dataInfo[index]; 
	}
	if (o[2].index) //   
	{
		var index = o[2].index;
		day = dataInfo[index];
	}
	if (o[3].index) //    
	{
		var index = o[3].index;
		hours = dataInfo[index];
	}
	if (o[4].index) //   
	{
		var index = o[4].index;
		minutes = dataInfo[index];
	}
	if (o[5].index) //   
	{
		var index = o[5].index;
		seconds = dataInfo[index];
	}
	if (o[6].index) //    
	{
		var index = o[6].index;
		milliseconds = dataInfo[index];
	}
	//    0   
	return new Date(year, month - 1, day, hours, minutes, seconds, milliseconds);
}
上より簡単な方法がありますが、IEはサポートしていません.
/*      ,   ,     ,      
               ,       
    
var data=new Date();
var  format ="yyyy-MM-dd HH:mm:ss";
var text=data.format(format);
alert(text);
var data2=parserToDate(text,format);
alert(data2.format(format));
 */
function parserToDate(dateText, format) {
	if (!dateText) { //  
		return new Date();
	}
  //     ,           	
	var defaultVal = new Date();
	var year = defaultVal.getFullYear();
	var month = defaultVal.getMonth() + 1;
	var day = defaultVal.getDate();
	var hours = defaultVal.getHours();
	var minutes = defaultVal.getMinutes();
	var seconds = defaultVal.getSeconds();
	var milliseconds = defaultVal.getMilliseconds();

	//           ,           
	var o = [{
			reg: "y+",
			regNew: "(?[0-9]{length})"
		}, //  
		{
			reg: "M+",
			regNew: "(?[0-9]{length})"
		}, //  
		{
			reg: "d+",
			regNew: "(?[0-9]{length})"
		}, // 
		{
			reg: "H+",
			regNew: "(?[0-9]{length})"
		}, //  
		{
			reg: "m+",
			regNew: "(?[0-9]{length})"
		}, // 
		{
			reg: "s+",
			regNew: "(?[0-9]{length})"
		}, // 
		{
			reg: "S",
			regNew: "(?[0-9]{length})"
		} //  
	];
	//          
	for (var i = 0; i < o.length; i++) {
		var onepart = o[i]
			var onepartReg = onepart.reg;
		var re = new RegExp("(" + onepartReg + ")");
		var res = re.exec(format);
		if (res) {
			var formatPart = res[0]; //         
			var regNew = onepart.regNew;
			var replaceVal = regNew.replace("length", formatPart.length);
			format = format.replace(formatPart, replaceVal);
		}
	}	
	var re = new RegExp(format);
	var res = re.exec(dateText);
	//       ,        
	if (res.groups) {
		var A1 = res.groups["A1"];
		if (A1) {
			year = A1;
		}
		var A2 = res.groups["A2"];
		if (A2) {
			month = A2;
		}
		var A3 = res.groups["A3"];
		if (A3) {
			day = A3;
		}
		var A4 = res.groups["A4"];
		if (A4) {
			hours = A4;
		}
		var A5 = res.groups["A5"];
		if (A5) {
			minutes = A5;
		}
		var A6 = res.groups["A6"];
		if (A6) {
			seconds = A6;
		}
		var A7 = res.groups["A7"];
		if (A7) {
			milliseconds = A7;
		}
	} else {
		alert(dateText + "     " + oldformat);
	}

	return new Date(year, month - 1, day, hours, minutes, seconds, milliseconds);
}