Convert Date String from YYYYMMDD to DD Mon YYYY

user500665

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.

nephiw

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.

edited at
0

Comments

0 comments
Login to comment

Related

Convert integer (YYYYMMDD) to date format (mm/dd/yyyy) in python

Convert dd-mm-yyyy string to date

Convert date from DD-MON-YY to YYYYMMDD

Reformatting a Date from DD-MMM-YYYY to YYYYDDMM or YYYYMMDD

how to convert YYYYMMDD to DD-Mon-YYYY in oracle?

Change date format from "yyyymmdd" to "mm/dd/yyyy"

Change date from YYYYMMDD to YYYY-MM-DD Javascript

How to convert a date format YYYY-MM-DD into integer YYYYMMDD in Presto/Hive?

Convert date format in R from "dd.mm.yyyy" to "yyyy"

Openquery convert yyyymmdd to MM/DD/YYYY

R How to change format of date from MM/DD/YYYY to YYYYMMDD and YY-Month (Abbreviated Month)?

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

converting string to date format like dd-mon-yyyy

Python: convert date format YYYY-mm-dd to dd-MON-yyyy with abbreviated month

coffeeScript date from String dd/mm/yyyy

convert yyyymmdd into yyyy/mm/dd in sybase

how to extract date present in dd-mon-yyyy format from string in oracle

Convert date from dd-mm-yyyy to mm/dd/yyyy

How to convert date format from dd-MMM-yyyy to yyyymmdd in Javascript?

How to convert string date (25.04.2016) to date format (mm/dd/yyyy)?

Convert YYYY-mm-dd to yyyymmdd in MYSQL

Convert date yyyyMMdd to yyyy-MM-dd'T'HH:mm:ss format with XSLT

convert a date in pig from YYYY-mm-dd to YYYYmmDD

Date Argument format conversion from YYYYMMDD to DD-MON-YYYY

How can I convert a date coming in as YYYYMMDD into MM/DD/YYYY or MM/D/YYYY depending on its length

Convert yyyymmdd chararray to yyyy/mm/dd date format

convert date from yyyy-mm-dd to dd month yyyy

How to convert a date as a string to "dd/mm/yyyy"?

Convert MM/dd/YYYY string date to datetime