js String to Date, js Date to String

Date.prototype.toFomatorString = function(formator)
{
var returnText = formator.toUpperCase();

if (returnText.indexOf("YYYY") > -1)
{
returnText = returnText.replace("YYYY", this.getYear());
}

if (returnText.indexOf("MM") > -1)
{
returnText = returnText.replace("MM", this.getMonth() + 1);
}

if (returnText.indexOf("DD") > -1)
{
returnText = returnText.replace("DD", this.getDate());
}

if (returnText.indexOf("HH") > -1)
{
returnText = returnText.replace("HH", this.getHours());
}

if (returnText.indexOf("MI") > -1)
{
returnText = returnText.replace("MI", this.getMinutes());
}

if (returnText.indexOf("SS") > -1)
{
returnText = returnText.replace("SS", this.getSeconds());
}

if (returnText.indexOf("SI") > -1)
{
returnText = returnText.replace("SI", this.getMilliseconds());
}

return returnText;
}


String.prototype.toDate = function()
{
var temp = this.toString();

temp = temp.replace(/-/g, "/");

var date = new Date(Date.parse(temp));

return date;
}

alert(new Date().toFomatorString("YYYY-MM-DD HH:MI:SS"));

alert("2012-05-31 23:01:34".toDate());

你可能感兴趣的:(Date,yyyy,tofomatorstring)