-2

I am working on JS code below in which .text(data.format_data.nextRechargeDate); returns the date in the following format: "2019-02-09T05:00:00"

Javascript Code:

this.$modal.find('.next-date').text(data.format_data.nextRechargeDate);

Problem Statement:

I am wondering what changes I need to make in the JS code above so that it returns the data format in the following way:

January 11, 2019

At this moment, as mentioned above it returns in the following format "2019-02-09T05:00:00"

flash
  • 1,564
  • 3
  • 27
  • 87

3 Answers3

1

With moment.js you can format the string date to the desired format as follows:

const formattedDate = moment("2019-02-09T05:00:00").format('MMMM DD, YYYY');

console.log(formattedDate); // "February 09, 2019"
<script src="https://unpkg.com/moment@2.23.0/min/moment.min.js"></script>
antonku
  • 5,707
  • 2
  • 11
  • 17
0

You can use the toLocaleDateString() method to format your date "2019-02-09T05:00:00" like this:

var x = new Date("2019-02-09T05:00:00"); //replace this with your retrieved modal text

var options = { month: 'long', day: '2-digit', year: 'numeric' };

x = x.toLocaleDateString("en-us", options);

console.log(x);
AndrewL64
  • 13,967
  • 6
  • 36
  • 64
0

I am not sure if this is what you are after, as the environment you are using is not exactly clear, but I hope this will help you get into the right direction

document.getElementById('next-date').value = moment().format().substr(0, 10);//(new Date()).toISOString().substr(0, 10);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>

<input type="date" id="next-date">
<button onclick="alert(moment(new Date(document.getElementById('next-date').value)).format('MMMM DD, YYYY'))">test</button>
Zlatin Zlatev
  • 2,670
  • 21
  • 30