jquery - How to remove timestamp using regex from a date string in javascript - Stack Overflow

New to regex.Need regular expression to match strings like T17:44:24Z from parent strings like 2015-12-

New to regex.

Need regular expression to match strings like T17:44:24Z from parent strings like 2015-12-22T17:44:24Z using javascript regex.

Once match is found will replace it with null or empty space.

To be specific i am trying to remove the time stamp from date string and obtain only date part.

Please help me in this

New to regex.

Need regular expression to match strings like T17:44:24Z from parent strings like 2015-12-22T17:44:24Z using javascript regex.

Once match is found will replace it with null or empty space.

To be specific i am trying to remove the time stamp from date string and obtain only date part.

Please help me in this

Share Improve this question asked Dec 23, 2015 at 12:25 KrankyCodeKrankyCode 4511 gold badge8 silver badges25 bronze badges 1
  • 1 Replace /T\d{2}:\d{2}:\d{2}Z/ with empty string. – ndnenkov Commented Dec 23, 2015 at 12:26
Add a ment  | 

2 Answers 2

Reset to default 2

You can use a simple regex like this:

T\d{2}:\d{2}:\d{2}Z
or
T(?:\d{2}:){2}\d{2}Z

Working demo

In case your T and Z are dynamic the, you can use:

[A-Z]\d{2}:\d{2}:\d{2}[A-Z]

Code

var re = /T\d{2}:\d{2}:\d{2}Z/g; 
var str = '2015-12-22T17:44:24Z';
var subst = ''; 

var result = str.replace(re, subst);

You don't need regex on this. You just split the string by T and get the second element from array, which would be 17:44:24Z in your case.

var date = '2015-12-22T17:44:24Z';
var result = date.split('T')[1];

If you also want to preserve T, you can just prepend it to the result:

var result = 'T' + date.split('T')[1]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745600278a4635360.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信