最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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]
发布评论

评论列表(0)

  1. 暂无评论