作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用以下函数将虚线日期 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;
}
作为测试,我在函数前后传入了 var myDate = '2013-12-11'
并注销,但格式保持不变?任何人都可以建议我可能在哪里出错吗?
这是一个测试jsFiddle:http://jsfiddle.net/wbnzt/
最佳答案
使用 String Replace 将破折号替换为斜杠。
string.replace(/-/g,"/")
关于javascript - 将日期字符串从破折号转换为正斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17219478/
我是一名优秀的程序员,十分优秀!