-5

I want this string:

var time = '02/19/2014';

to look like:

var time = '20140219';

That is, I want the format mm/dd/yyyy to "be regexed" into yyyymmdd.

Weblurk
  • 6,279
  • 16
  • 58
  • 109
  • Why not convert it with the Date object rather then a Regex? – putvande Feb 14 '14 at 13:34
  • 2
    Why a RE? `time = time.substr(6, 4) + time.substr(0, 2) + time.substr(3, 2)` – Alex K. Feb 14 '14 at 13:35
  • @AlexK. This worked like a charm. I thought I had to use regex for this but this was much cleaner. If you write it as an answer I can accept it. I know it doesn't answer my question per se, since I asked for regex but I think it's important to show that the questionaire thought regex was necessary but someone provided a better solution. – Weblurk Feb 14 '14 at 13:44

3 Answers3

2

Try:

var time = '02/19/2014';
time = time.replace(/(\d+)\/(\d+)\/(\d+)/, '$3$1$2');
lshettyl
  • 8,016
  • 4
  • 23
  • 30
1

How about some simple string manipulation?

time = time.substr(6, 4) + time.substr(0, 2) + time.substr(3, 2);
Alex K.
  • 165,803
  • 30
  • 257
  • 277
0

As you can see, string consists of three blocks, divided by slashes. I propose you to do the following: pattern as (\d{2}) / (\d{2}) / (\d{4}), and replacement as $3$1$2 (without spaces). It is written in php, but I hope that you can easily convert it into your language.

user3162968
  • 926
  • 1
  • 8
  • 16