最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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]
Post a comment

comment list (0)

  1. No comments so far