admin管理员组文章数量:1291041
I'm trying to convert a date in javascript from MM/dd/yyyy to yyyy/MM/dd
so this works:
var d = new Date("08/08/2012");
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
output = 2012/7/8
///////////////////////////////////////////////////
this does not:
var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
and neither does this:
var dateString = "08/08/2012";
var d = Date.parse(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
how do I make it work with a string variable? thanks
~Myy
I'm trying to convert a date in javascript from MM/dd/yyyy to yyyy/MM/dd
so this works:
var d = new Date("08/08/2012");
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
output = 2012/7/8
///////////////////////////////////////////////////
this does not:
var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
and neither does this:
var dateString = "08/08/2012";
var d = Date.parse(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
how do I make it work with a string variable? thanks
~Myy
Share Improve this question asked Aug 9, 2012 at 20:17 MyyMyy 18.5k11 gold badges40 silver badges57 bronze badges 3- What does the browser's console log when it tries to execute the code that doesn't work? – Jordan Commented Aug 9, 2012 at 20:22
- Are you saying that you want 08/08/2012 to print as 2012/7/8 (i.e., with the month changed from 08 to 7)? – Marcelo Cantos Commented Feb 28, 2013 at 0:35
- no, I was having a problem using a string variable vs the new Date() function. – Myy Commented Mar 2, 2013 at 17:22
3 Answers
Reset to default 5var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + d.getMonth() + "/" + d.getDate();
document.write(dateString);
That should, and does, work. Keep in mind that JavaScript stores months as a zero-indexed value.
If you want to have leading zeros, then you'll have to do some magic:
var dateString = "08/08/2012";
var d = new Date(dateString);
dateString = d.getFullYear() + "/" + ('0' + (d.getMonth()+1)).slice(-2) + "/" + ('0' + d.getDate()).slice(-2);
document.write(dateString);
jsFiddle
The reason why your Date.parse( )
example is not working, is because that function returns a timestamp (number of milliseconds since 1970), instead of a Date object. Therefore, you can't call functions like getFullYear()
on the timestamp.
If all you need to do is re-order the values, you can do:
var dateString = "08/08/2012";
var dateElements = dateString.split("/");
var outputDateString = dateElements[2] + "/" + dateElements[0] + "/" + dateElements[1];
document.write(outputDateString );
I can confirm with MrSlayer that the code works in jsFiddle.
Your attempts at using Date.parse()
should actuall be using Date(String(dateString))
.
Don't forget to add 1 for each month.
本文标签: javascript date with stringStack Overflow
版权声明:本文标题:javascript date with string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741503526a2382187.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论