1

I have a 48 hour timeline. I'm utilizing the JavaScript date function. I am looking for the following format and I am not sure how to achieve it. I want to display today's date and the next day.

DD - DD+1 MMM YY

    <script> 
        var d = new Date();
        var n = d.getDate();
        document.write(n);

    </script>

http://jsfiddle.net/deaconf19/bGm6c/

Thanks

JA1
  • 538
  • 2
  • 7
  • 21

4 Answers4

4
var d = new Date();

var month = new Array();
month[0]="Jan";
month[1]="Feb";
month[2]="Mar";
month[3]="Apr";
month[4]="May";
month[5]="Jun";
month[6]="Jul";
month[7]="Aug";
month[8]="Sep";
month[9]="Oct";
month[10]="Nov";
month[11]="Dec";

var m = month[d.getMonth()];
var d1 = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var m1 = month[d1.getMonth()];

$("#date").append(d.getDate() + " " + m + " " + d.getFullYear() + " - " + d1.getDate() + " " + m1 + " " + d1.getFullYear());

Here take a look http://jsfiddle.net/N0ir/bGm6c/4/

OutFall
  • 482
  • 7
  • 20
  • This doesn't handle end-of-the-month or end-of-the-year dates. For instance, if the current day is April 30 (remember there's no 31st day in April), then the output for this is incorrectly `30 - 31 Apr 2014`. Likewise, for New Year's Eve (Dec. 31), it is `31 - 32 Dec 2014`. This is because [`Date.prototype.getDate`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate) returns the *day-of-the-month number*, and nothing else. – ajp15243 Apr 24 '14 at 16:25
  • @ajp15243 very noob of me not to see it, made adjustments – OutFall Apr 24 '14 at 16:38
  • 1
    @N0ir Well, now it says `30 - 1 Apr 2014` :) – ajp15243 Apr 24 '14 at 16:42
  • You'll need to make either a ternary condition to see if d1 is less than d in the new code. Then if so change month or don't add it at all. Least that's what I would do – EasyBB Apr 24 '14 at 16:44
  • @N0ir +1 Looks to work to me! Although, if you want to get really technical, I just remembered that leap years are still something to consider, but there's a lot more logic to checking if the current year is a leap year. – ajp15243 Apr 24 '14 at 17:02
  • @ajp15243 It should cover leap years too. [28 Feb 2015 - 1 Mar 2015] and [28 Feb 2016 - 29 Feb 2016] – OutFall Apr 24 '14 at 17:09
  • @N0ir Oh yes, you are right, that logic would be handled by `Date`'s constructor when instantiating `d1`. – ajp15243 Apr 24 '14 at 17:14
  • thanks for the update I was going to post how this is showing 32 days instead of the first of next month – JA1 Apr 30 '14 at 18:06
3

Try this

var tomorrow= new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
Lasitha Benaragama
  • 2,201
  • 2
  • 27
  • 43
1
    <script> 
            var d = new Date();
            var n = d.getDate(); 
            var tomorrow = new Date();
            tomorrow.setDate(n+1);
            document.write(n);
            document.write(" ");
            document.write(tomorrow.getDate());
    </script>
Timur
  • 639
  • 6
  • 21
  • @JeremyA1 - At least it is until the end of the month, but you're always welcome back in 6 days. – adeneo Apr 24 '14 at 16:10
  • @adeneno - You are correct it now displays 32 days instead of first of the month. I found a fix on here but when I implement it it shows a long string like a ticker – JA1 Apr 30 '14 at 18:04
  • hm, strage - it works for me -> 30 and then 01 http://jsfiddle.net/acidrat/f8D4h/ but anyway you have already found your answer. – Timur May 01 '14 at 16:00
0

Assume you have your date in d:

d.setDate(d.getDate() + 1); // add 1 day to d

This is basically the in situ variation of @ambarox' answer.

The generalizations should be obvious ...

The standalone version for the op's code:

<script> 
    var d = new Date()
      , t = new Date()
      ;

    t.setDate(d.getDate() + 1);
    if (d.getMonth() === t.getMonth()) {
        document.write(d.getDay() + " - ");
    }
    else {
        if (d.getYear() === t.getYear()) {
            document.write(d.getDay() + "." + d.getMonth());
        }
        else {
            document.write(d.getDay() + "." + d.getMonth() + "." + d.getYear());
        }
    }
    document.write(" - " + t);
</script>

More details on how to add/subtract different time and date values and on what methods are available to do are cited in this SO answer.

Community
  • 1
  • 1
collapsar
  • 17,010
  • 4
  • 35
  • 61