1

how is it possble to get the date in this format ? 28/09/2013

what i am getting now is,

Fri Sep 27 2013 15:19:01 GMT+0530 (Sri Lanka Standard Time)

This is the code i have written to get that..

 var date = new Date();
 var tomorrow = new Date(date.getTime() + 24 * 60 * 60 * 1000);
 alert(tomorrow);

and i need to see weather, is the given date is tomorrow. something like this when i give 28/09/2013 it should alert as tomorrow or not.

any help is highlight appreciated.

NOTE : i only need to compare with date. 28/09/2013 === tomorrow

dev1234
  • 5,376
  • 15
  • 56
  • 115
  • 3
    Comparing dates in JS is a nightmare. Use [date.js](http://www.datejs.com/) to simplify it. – Rory McCrossan Sep 26 '13 at 10:13
  • 1
    @RoryMcCrossan ok. but how to get only date from this ? Fri Sep 27 2013 15:19:01 GMT+0530 (Sri Lanka Standard Time) – dev1234 Sep 26 '13 at 10:14
  • 1
    Why would you want to use jquery, a dom manipulation library, to compare dates – Lepidosteus Sep 26 '13 at 10:17
  • 1
    @mazraara check on the documentation of date.js, specifically the `toString()` method. – Rory McCrossan Sep 26 '13 at 10:19
  • 1
    @Lepidosteus is there any other possibility ? – dev1234 Sep 26 '13 at 10:19
  • @mazraara the Date() object is not part of jquery. It's javascript. There is not a single word in your question that is jquery specific. Jquery is a javascript library for dom manipulation (adding a div, changing the color of an element, ...). I recommend you read a good javascript documentation to avoid mixing the two together. – Lepidosteus Sep 26 '13 at 10:26
  • @Lepidosteus tnx a lot. btw do u knw hw can i check weather two dates are equal or not via javascript? – dev1234 Sep 26 '13 at 10:58

4 Answers4

7

You can try following to get the next day :

var myDate=new Date();
myDate.setDate(myDate.getDate()+1);
// format a date
var dt = myDate.getDate() + '/' + ("0" + (myDate.getMonth() + 1)).slice(-2) + '/' + myDate.getFullYear();
console.log(dt);

Here is the demo : http://jsfiddle.net/5Yj3V/3/

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • 1
    will it work on a situation like this ? lets say today is the last day of month of September if i take tomrrow will it print 30 or October 1st ? – dev1234 Sep 26 '13 at 10:28
  • yes, it will. It will return you October 1st. Infact you can try it, change the difference to 5 and check. Here is the [demo](http://jsfiddle.net/5Yj3V/1/) – Nishu Tayal Sep 26 '13 at 10:35
  • 1
    there is a problem... the month is returning as one digit so this causes a mismatch... 28/09/2013 --- 27/9/2013.. it should be 09 not just 9. – dev1234 Sep 26 '13 at 10:57
  • 1
    @mazraara: updated fiddle and answer as you want. Use : `("0" + (myDate.getMonth() + 1)).slice(-2)` it'll give month in 2-digit format – Nishu Tayal Sep 26 '13 at 11:01
6

Moment.js will do that for you very easily.

moment().add('days', 1).format('L');
Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43
4

I would use the DateJS library.

var tomorrow = new Date.today().addDays(1).toString("dd-mm-yyyy"); 
Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
1

Try the below fiddle using javascript.

var tomorrow = new Date(); 
var newdate = new Date();
var month = (newdate.getMonth()+1);
newdate.setDate(tomorrow.getDate() + 1);
if (month < 10)
{
    month = '0' + (newdate.getMonth()+1);
}

alert(newdate);
alert(newdate.getDate() + '/' + month + '/' + newdate.getFullYear());
Saranya Sadhasivam
  • 1,296
  • 6
  • 9
  • 1
    will it work on a situation like this ? lets say today is the last day of month of September if i take tomrrow will it print 30 or October 1st ? – dev1234 Sep 26 '13 at 10:27
  • 1
    Since getMonth() returns 0-11,you'll have to increment the value by 1 i.e. `newdate.getMonth()+1` – Nishu Tayal Sep 26 '13 at 10:32
  • 1
    @SaranyaSadhasivam there is a problem... the month is returning as one digit so this causes a mismatch... 28/09/2013 --- 27/9/2013.. it should be 09 not just 9. – dev1234 Sep 26 '13 at 10:57