I have a date as a string "20180619"
How can I convert this to 19 Jun 2018
I started by trying
var date = new Date(parseInt("20180619"));
var d = date.getDate();
var m = date.getMonth();
var y = date.getFullYear();
console.log(d + ' ' + m + ' ' + y)
But get 1 0 1970
Edit: So there are actually 2 issues here, first is the date is the wrong format, and second get the month name. The second part is answer by the other linked question. So it just boils down to splitting the date down to it's components using one of a couple of different methods in the answers here.
You will need to parse your string into something that Date can understand. How about:
var dateString = "20180619";
var parsedDate = dateString.replace(/(\d{4})(\d{2})(\d{2})/, '$1/$2/$3');
var date = new Date(parsedDate);
var d = date.getDate();
var months = ['Jan', 'Feb', 'Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var m = months[date.getMonth()];
var y = date.getFullYear();
console.log(d + ' ' + m + ' ' + y)
The result is 19 Jun 2018
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments