junaidp
junaidp

Reputation: 11201

change new date format

I am trying to get today's date with some specific format:

var d = new Date();
var curr_year = d.getFullYear();
var curr_Month = d.getMonth();
var curr_date = d.getDate();
var todayDate =   (curr_year +"/"+curr_Month +"/"+ curr_date );

This is not working. What is the proper way of changing my date's format?

Upvotes: 2

Views: 1888

Answers (2)

James Hill
James Hill

Reputation: 61812

In JavaScript, the months are zero based. For example:

January = 0

February = 1

...

Simply add one to your month:

var d = new Date();
var curr_year = d.getFullYear();
var curr_Month = d.getMonth() + 1; // +1 to zero based month
var curr_date = d.getDate();
var todayDate =   (curr_year +"/"+curr_Month+"/"+ curr_date );

Here's a working fiddle.

Upvotes: 3

Haim Evgi
Haim Evgi

Reputation: 125496

var curr_Month = d.getMonth() + 1; //months are zero based

Upvotes: 3

Related Questions