MMR
MMR

Reputation: 3009

How to convert date format in js?

I am getting the date format as

Sat Jan 28 2017 05:30:00 GMT+0530 (IST)

I want this date to be like

January 2017

Can anyone please suggest help.Thanks.

Upvotes: 2

Views: 151

Answers (5)

Chand Ra
Chand Ra

Reputation: 69

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

var str=arr[d.getMonth()]+""+d.getFullYear();

console.log(str);

Upvotes: 1

Stanislav E. Govorov
Stanislav E. Govorov

Reputation: 161

Consider using moment.js for formatting/parsing dates. This will allow you to store input/output format in config and not hardcode parser implementation. http://momentjs.com/docs/#/parsing/

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34914

var date = new Date('Sat Jan 28 2017 05:30:00 GMT+0530 (IST)');
var months = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"
];
console.log(months[date.getMonth()],date.getFullYear());

Upvotes: 1

rishipuri
rishipuri

Reputation: 1528

You can use the toLocaleString() on Date object

date = new Date();

newDate = date.toLocaleString('en-US', {year: 'numeric', month: 'long'});

console.log(newDate);

More info here: Date.prototype.toLocaleString()

Upvotes: 8

Angular San
Angular San

Reputation: 356

TRY THIS :D

function myFunction() {
    var month = new Array();
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";

    var d = new Date();
    var month = month[d.getMonth()];
    var year = d.getFullYear();
    document.getElementById("demo").innerHTML = month+year;
}
<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

Upvotes: 1

Related Questions