how to conver date like 01 mar 2011 to only mar 2011 [closed]
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
开发者_开发技巧 Improve this questionhow to conver date like 01 mar 2011 to only mar 2011 in javascript or jquery
You could split it on .
var date = '01 mar 2011';
var dateTokens = date.split(' ');
dateTokens.shift();
var newDate = dateTokens.join(' ');
jsFiddle.
Alternatively you could use a regex...
var newDate = date.replace(/\d+ /, '');
document.body.innerHTML = newDate;
jsFiddle.
If you have this as a string you cansubstr
-it from the first space onwards. Like this:
var d = "01 Mar 2011";
var formatted = d.substr(d.indexOf(" "), d.length);
Otherwise (if it's not a string) you'll need to use the Date
object.
Similar to the others:
function trimDate(s) {
return s.replace(/^[\d ]+/,'');
}
A more efficient (though longer) version:
var trimDate = (function() {
var re = /^[\d ]+/;
return function (s) {
return s.replace(re, '');
}
}());
alert(trimDate('1 mar 2011')); // mar 2011
精彩评论