0

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.

user500665
  • 1,220
  • 1
  • 12
  • 38
  • 2
    read [Date documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) to see where your first line of code went wrong - as much as "libraries" are off topic, use moment.js - you wont regret it – Jaromanda X Jun 19 '18 at 00:10
  • 2
    Possible duplicate of [Get month name from Date](https://stackoverflow.com/questions/1643320/get-month-name-from-date). This link looks like it would solve most of your problem to me. – Tim Biegeleisen Jun 19 '18 at 00:10
  • `new Date("20180619".replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3')).toDateString().replace(/\w+\s(\w+)\s(\w+)\s(\w+)/, '$2 $1 $3')` – Jaromanda X Jun 19 '18 at 00:16

5 Answers5

2

I've got a new simple solution with toLocaleString :

var str = '20180619';

var date = new Date([str.slice(0, 4), str.slice(4, 6), str.slice(6, 8)].join('/'));

var result = date.toLocaleString('en-GB', { month: 'short', year: "numeric", day: "2-digit"})

console.log(result);
Terry Wei
  • 1,521
  • 8
  • 16
1

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

nephiw
  • 1,964
  • 18
  • 38
1

Construct date object in given pattern i.e. yyyy-mm-dd or dd-mm-yyyy, where separators can be - or /

var date = new Date("20180619".replace(/(\d{4})(\d{2})(\d{2})/, '$1-$2-$3'));
var str = [date.getDate(), (['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][date.getMonth()]), date.getFullYear()].join(' ');
console.log(str);

var str = "20180619".replace(/(\d{4})(\d{2})(\d{2})/, function(o, y, m, d) {
  return [d, ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][parseInt(m, 10) - 1], y].join(' ');
});
console.log(str);
0

Convert/Split your date string to an Array and than use array index to get value and create Date object.

Here I have created a simple JSFiddle for you.

var dateString = "20180619";
var dateArray = dateString.split("");

console.log(dateArray);

var myDate = new Date();
console.log(myDate.toString());

myDate.setFullYear(dateArray[0] + dateArray[1] + dateArray[2] + dateArray[3]);

myDate.setMonth(dateArray[4] + dateArray[5]);

myDate.setDate(dateArray[6] + dateArray[7]);

console.log(myDate.toString());

Formatting of date part is still missing. I hope you will be able to work that yourself.

Zeeshan Elahi
  • 247
  • 2
  • 9
  • I'm not sure how to get an array but I could get each part like `date.slice(6, 8)` Or is there a better method you are thinking of? – user500665 Jun 19 '18 at 00:21
-1
var date = new Date("06/19/2018"); //You were providing the date in a wrong format.

const monthNames = ["January", "February", "March","April","May","June","July","August","September","October","November","December"];

var newDate = date.getDate() + ' ' + monthNames[date.getMonth()] + ' ' + date.getFullYear();