我有这个字符串......
var myDate = "November 15, 2010";
.....我需要这样做......
$("#request_showdate_1i").val('2010');
$("#request_showdate_2i").val('November');
$("#request_showdate_3i").val('15');
任何想法如何打破这个字符串?
答案 0 :(得分:7)
例如,您可以使用正则表达式:
var parts = /(\S+) (\d+), (\d+)/.exec(myDate);
$("#request_showdate_1i").val(parts[3]);
$("#request_showdate_2i").val(parts[1]);
$("#request_showdate_3i").val(parts[2]);
答案 1 :(得分:3)
您可以使用Date
对象example on jsFiddle
var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var d = new Date("November 15, 2010");
$("#request_showdate_1i").val(d.getFullYear()); // 2010
$("#request_showdate_2i").val(months[d.getMonth()]); // November from array
$("#request_showdate_3i").val(d.getDate()); // 15
答案 2 :(得分:1)
@ Guffa的答案似乎值得一试,但您是否考虑过使用Javascript的 datepart 功能?
3个div来保存不同的日期部分
<div class="day"></div>
<div class="month"></div>
<div class="year"></div>
还有一些Javascript魔法
var myDate = new Date("November 15, 2010");
var month_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
$(".day").text(myDate.getDate());
$(".month").text(month_names[myDate.getMonth()]);
$(".year").text(myDate.getFullYear());