1

i'm passing start date as 7/02/2014 in dd/mm/yyyy format, to add one month extra for current passed date, first i need to convert the passed var item to Date format and then i can manipulate on it.

if i try to get date like this, var newDate = new Date(startDate.val()); then i will be getting new date as 7 july 2014, i.e format of date i'm getting in response is mm/dd/yyyy.

i need output of date() to be in dd/mm/yyyy format only. how to achieve this?

Is there any other function in jquery to do this?

0

4 Answers 4

3
var dateArr = '7/02/2014'.split('/');
var date = new Date();
date.setYear(dateArr[2]);
date.setMonth(dateArr[1] -1); //month starts from 0
date.setDate(dateArr[0]);
Sign up to request clarification or add additional context in comments.

3 Comments

What is month starts from 0?, i didnt understand
this means , that date.setMonth(0) will set month to January , date.setMonth(2) - March etc.
Oh, i understood +1 for your comment. Thanks
3

Use jquery

var date = $.format.date(new Date(Date), 'dd/mm/yyyy'); 

https://github.com/phstc/jquery-dateFormat

4 Comments

inner Date is of type string?
var Date = startDate.val() ; $.format.date(new Date(Date), 'dd/mm/yyyy');
oh, i think i need to install plugin for this :(, else i get error "Microsoft JScript runtime error: Object doesn't support this property or method"
yea required , //code.jquery.com/ui/1.10.4/jquery-ui.js
0

Actually I recommend jQuery Datepicker. Because really simple and comprehensible.

You dont have any edits because datepicker default options show like this : dd/mm/yyyy

jQuery;

 $(function() {
   $( "#datepicker" ).datepicker();
 });

Html;

 <p>Date: <input type="text" id="datepicker"></p>

Check it : http://jqueryui.com/datepicker/

Comments

0

You can try this one

var newDate = new Date("2/7/2014");
var dateformat = newDate.getDate();
dateformat+="/";
dateformat+=newDate.getMonth()+1;
dateformat+="/";
dateformat+=newDate.getYear();
alert(dateformat);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.