-1

Im try to do using html convert to full name of month but i cant correctly do that , anyone know how to do that i need to 21-September-2017 please help me to fix this thanks,

function init(){
    var d = new Date();
    var day = d.getDate();
    var x = d.toDateString().substr(4, 3);
    var year = d.getFullYear();
    document.querySelector("#date").innerHTML = day + '-' + x + '-' + year;
}
window.onload = init;
<div id="date"></div>
putvande
  • 15,068
  • 3
  • 34
  • 50
core114
  • 5,155
  • 16
  • 92
  • 189

3 Answers3

2

You can use toLocaleString() to get month.

function init(){
    var d = new Date();
    var day = d.getDate();
    var locale = "en-us";
    var month = d.toLocaleString(locale, { month: "long" });
    var year = d.getFullYear();
    document.querySelector("#date").innerHTML = day + '-' + month + '-' + year;
}
window.onload = init;
<div id="date"></div>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

You can not get full month name directly. But you can do a manual implementation like this,

var d = new Date();
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 n1 = month[d.getMonth()];
var day = d.getDate();
var year = d.getFullYear();
var final_date = day+"-"+n1+"-"+year;

FIDDLE

Parag Jadhav
  • 1,853
  • 2
  • 24
  • 41
0

Use Date.prototype.toLocaleString

It converts your date to the locale format used by the language and country code specified. Only usable in up to date browsers.

function init(){
    var d = new Date();
    var options = {day: 'numeric', month: 'long', year: 'numeric' };
    document.querySelector("#date").innerHTML = d.toLocaleString('en-US', options);
}
window.onload = init;
<div id="date"></div>

This will use the current time zone of the client. To negate this use these options: options.timeZone = 'UTC'; options.timeZoneName = 'short';

Mouser
  • 13,132
  • 3
  • 28
  • 54