Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
91
rated 0 times [  98] [ 7]  / answers: 1 / hits: 46642  / 9 Years ago, thu, april 9, 2015, 12:00:00

I am getting from api xml data, where one of the xml elements is date time, which nodeValue is always in this format - string: YYYY-MM-DD. (I can not request from api, to return me date time in diffrent format)



My problem is to split and convert this format into this string: DD.MM.YYYY



Basicly I did this:



var myString = 2015-04-10; //xml nodeValue from time element
var array = new Array();

//split string and store it into array
array = myString.split('-');

//from array concatenate into new date string format: DD.MM.YYYY
var newDate = (array[2] + . + array[1] + . + array[0]);

console.log(newDate);


Here is jsfiddle: http://jsfiddle.net/wyxvbywf/



Now, this code works, but my question is: Is there a way to get same result in fewer steps?


More From » string

 Answers
4

should do the same



var newDate = '2015-04-10'.split('-').reverse().join('.')
// ^ ^ ^ join to 10.04.2015
// | |reverses (2 -> 0, 1 -> 1, 0 -> 2)
// | delivers Array

[#67134] Tuesday, April 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazmyne

Total Points: 503
Total Questions: 102
Total Answers: 99

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
jazmyne questions
;