开发者

how to conver date like 01 mar 2011 to only mar 2011 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

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 question

how 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    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜