JavaScript Javascript converting YYMMDD formatted string into date object javascript convert yyyymmdd to date,javascript convert yyyymmdd to yyyy-

This is probably a trivial question, but I'm trying to look for the most efficient way to do this.

I am given a string in this format "20130914" from a servlet and I need to create a javascript date object from it.

From what I know, date object accepts ISO format for strings so I need to append dashes in between my year,month and date like this "2013-09-14"

I could use .splice function to insert dashes in between the year,month and date but I feel like there has to be an easier solution for this trivial question. Or am I overthinking this?

Answer:1

What you're doing is fine, there is no big problem in writing simple code to do common tasks.

If you find yourself performing a lot of date calculations momentjs is a very small (5kb) library that handles dates just like that.

For example, what you need in moment can be written as:

moment("20130914", "YYYYMMDD");
Answer:2

Because I am in love with regex:

var rawDate = '20130914';
console.log(new Date(rawDate.replace(/(\d{4})(\d{2})(\d{2})/,"$1-$2-$3")));
//Output: Sat Sep 14 2013 12:00:00 GMT+1200 (New Zealand Standard Time) 
Answer:3

Using plain and simple JS:

var input = "20130914";
var date = new Date(input.substr(0, 4), input.substr(4, 2) - 1, input.substr(6, 2));
console.log(date.toString());
Answer:4

With JavaScript or CoffeeScript, is there an easy way to test for the following: if (locals.messages.text === "somestring") { ... } Now this needs three things to be true: locals needs to exist, ...

With JavaScript or CoffeeScript, is there an easy way to test for the following: if (locals.messages.text === "somestring") { ... } Now this needs three things to be true: locals needs to exist, ...

I have a webpage aligned properly when viewed fullscreen in a browser, but when the same is resized things go horribly mis-aligned. Here's the html. <html> <head> <style type='text/css'...

I have a webpage aligned properly when viewed fullscreen in a browser, but when the same is resized things go horribly mis-aligned. Here's the html. <html> <head> <style type='text/css'...

function Class () { var self = this; this.hi = true; // this { hi: true }, self { hi: true } self.hi = false; // this { hi: false }, self { hi: false } } Why doesn't self behave like a normal var?

function Class () { var self = this; this.hi = true; // this { hi: true }, self { hi: true } self.hi = false; // this { hi: false }, self { hi: false } } Why doesn't self behave like a normal var?

I have a web form, with text inputs for crontab values (minutes/hours/months/days), and I want to display user the next date of selected crontab task execution. For example, i have a values 20 15 15 *...

I have a web form, with text inputs for crontab values (minutes/hours/months/days), and I want to display user the next date of selected crontab task execution. For example, i have a values 20 15 15 *...