0

I have this code:

var fd=1+self.theDate.getMonth() +'/'+ today+'/'+self.theDate.getFullYear();

It works, but it's format is Month, Day, Year.

I need to change it to: Day, Month Year.

So, I tried this:

var fd=1+today +'/'+ self.theDate.getMonth()+'/'+self.theDate.getFullYear();

Now, my change does not work. Is it that I have not done it properly or is my change right?

Thanks

0

3 Answers 3

1

I expect the correct answer is this:

var fd=today +'/'+ (self.theDate.getMonth() + 1) +'/'+self.theDate.getFullYear();

This leaves today alone, and groups Month so that it does a proper number addition instead of string concatenation.

Sign up to request clarification or add additional context in comments.

Comments

1
var theDate = new Date();
var today = theDate.getDate();
var month = theDate.getMonth()+1; // js months are 0 based
var year = theDate.getFullYear();
var fd=today +'/'+ month +'/'+year

or perhaps you prefer 22/05/2011

var theDate = new Date();
var today = theDate.getDate();
if (today<10) today="0"+today;
var month = theDate.getMonth()+1; // js months are 0 based
if (month < 10) month = "0"+month;
var year = theDate.getFullYear();
var fd=""+today +"/"+ month +"/"+year

Comments

1

You are no longer adding 1 to the month, you are adding it to today. Make sure to parenthesize this since "x" + 1 + 2 => "x12" but "x" + (1 + 2) => "x3"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.