我正在尝试使用以下函数将虚线日期 2013-12-11 转换为 2013/12/11:
function convertDate(stringdate)
{
// Internet Explorer does not like dashes in dates when converting,
// so lets use a regular expression to get the year, month, and day
var DateRegex = /([^-]*)-([^-]*)-([^-]*)/;
var DateRegexResult = stringdate.match(DateRegex);
var DateResult;
var StringDateResult = "";
// try creating a new date in a format that both Firefox and Internet Explorer understand
try
{
DateResult = new Date(DateRegexResult[2]+"/"+DateRegexResult[3]+"/"+DateRegexResult[1]);
}
// if there is an error, catch it and try to set the date result using a simple conversion
catch(err)
{
DateResult = new Date(stringdate);
}
// format the date properly for viewing
StringDateResult = (DateResult.getMonth()+1)+"/"+(DateResult.getDate()+1)+"/"+(DateResult.getFullYear());
console.log(StringDateResult);
return StringDateResult;
}
作为测试我通过了 varmyDate = '2013-12-11'
在函数之前和之后登录和注销但格式保持不变?谁能建议我可能在哪里出错?
这是一个测试jsFiddle: http://jsfiddle.net/wbnzt/
使用字符串替换将破折号替换为斜杠。
string.replace(/-/g,"/")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)