7

I'm trying to convert a string from my database that is formatted as YYYYMMDD and is placed in the input box id="startingdate". How would I convert it to YYYY-MM-DD at the other input box id="startingdate1"?

<input type="" id="startingdate1" name="startingdate1" size="20" class="form-control col-md-7 col-xs-12" placeholder="YYYY-MM-DD"/>
<input id="startingdate" name="startingdate" type="hidden"/>
2
  • I think you should use momentjs Commented Apr 15, 2016 at 4:14
  • @NguyenSyThanhSon—it doesn't seem sensible to load a few thousand lines of library, convert the string to a date, then convert it back to a string again in a slightly different format just to insert two hyphens. Commented Apr 15, 2016 at 4:26

4 Answers 4

19
s = s.replace(/(\d{4})(\d{2})(\d{2})/g, '$1-$2-$3');

This uses a regular expression to replace a sequence of four digits, followed by a sequence of two digits, followed by a sequence of two digits, with the first, plus a -, plus the second, plus a -, plus the third.

Sign up to request clarification or add additional context in comments.

6 Comments

I just love when I'm trying to solve a problem concerning string manipulation, and then I remember -- "Oh yeah, there's a whole language dedicated to parsing strings"
@4castle Thank you so much actually i m beginner i m trying to improve my skills thanks for suggestion
This didn't work in my js browser console, nor my node console. any reason why?
s = "YYYYMMDD"; s = s.replace(/(\d{4})(\d{2})(\d{2})/g, '$1-$2-$3'); console.log(s);
s = '20160416'; // here use digits insteads of words s = s.replace(/(\d{4})(\d{2})(\d{2})/g, '$1-$2-$3'); // if you want words the replace the d with w console.log(s);
|
5

Not using a regular expression if for some reason you don't want to:

var startDate = document.getElementById('startingdate').value;
var displayDate = document.getElementById('startingdate1');

var year = startDate.substring(0, 4);
var month = startDate.substring(4, 6);
var day = startDate.substring(6, 8);

displayDate.value = year + '-' + month + '-' + day;
<input type="" id="startingdate1" name="startingdate1" size="20" class="form-control col-md-7 col-xs-12" placeholder="YYYY-MM-DD"/>
<input id="startingdate" name="startingdate" type="hidden" value="20160415"/>

3 Comments

Or even [s.slice(0, 4), s.slice(4,6), s.slice(6,8)].join('-'). ;-)
@RobG That's actually the best solution to this question, but regex is pretty awesome too.
as a very new programmer, your answer helped me in understanding the logic to handle this problem the most. So, thank you!
3

The most reliable way for me so far was:

var a = "YYYYMMDD";
var b = [a.slice(0, 4), "-", a.slice(4, 6), "-", a.slice(6, 8)].join('');
console.log(b);

1 Comment

If you remove the - from inside the array, you can just put .join('-')
3

Cleaner, simpler version:

function dateFormat(value, pattern) {
  var i = 0,
    date = value.toString();
  return pattern.replace(/#/g, _ => date[i++]);
}
var displayDate = dateFormat('19700101', '####-##-##');
console.log(displayDate);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.