1

I am attempting to display a greeting before the date / time depending on what time of day it is.

  1. Good Morning
  2. Good Afternoon
  3. Good Evening

I have that message working.

I also want to have the date and time displayed as "It is TIME on DATE."

Whenever I try to change something with the time date code I can no longer get it to display even that.

Any advice would be helpful.

function MakeArray(n) {
    this.length = n;
}

monthNames = new MakeArray(13);
monthNames[1] = "January";
monthNames[2] = "February";
monthNames[3] = "March";
monthNames[4] = "April";
monthNames[5] = "May";
monthNames[6] = "June";
monthNames[7] = "July";
monthNames[8] = "August";
monthNames[9] = "September";
monthNames[10] = "October";
monthNames[11] = "November";
monthNames[12] = "December";

dayNames = new MakeArray(8);
dayNames[1] = "Sunday";
dayNames[2] = "Monday";
dayNames[3] = "Tuesday";
dayNames[4] = "Wednesday";
dayNames[5] = "Thursday";
dayNames[6] = "Friday";
dayNames[7] = "Saturday";

function dayPart(oneDate) {
    var theHour = oneDate.getHours();

    if (theHour < 12) {
        return "Good morning";
    }

    if (theHour < 18) {
        return "Good afternoon";
    }

    return "Good evening";
}

function customDateString(oneDate) {
    var theDay = dayNames[oneDate.getDay() + 1],
        theMonth = monthNames[oneDate.getMonth() + 1],
        theYear = oneDate.getYear();

    theYear += (theYear < 100) ? 1900 : 0;

    return theDay + ", " + theMonth + " " + oneDate.getDate() + ", " + theYear;
}

var today = new Date();

alert(dayPart(today) + "." + customDateString(today));

On jsFiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79
  • You don't need `return this;` in your `constructor`. In javascript array indexes begin at `0`, if you want to use any generic methods on yours then you should consider that too. So your lengths should be `13` and `8` as `0` is a `hole` in your `sparse` arrays. – Xotic750 Feb 14 '14 at 03:52
  • Appreciate that help. I'm not sure where I'm going wrong on the output format. I also need to add the time in there but I got it to this point and any change I try to make ends up messing up what output I do have. It's all new to me so just trying to make sense of how my changes affect the rest of it. – user3199950 Feb 14 '14 at 04:00
  • What is the rationale behind `theYear += (theYear < 100) ? 1900 : 0;`? – Xotic750 Feb 14 '14 at 04:03
  • Another little tip, [jsFiddle](http://jsfiddle.net/Xotic750/5fpke/) is a wonderful playground. And MDN has good documents [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)- Have a look at [getFullYear](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getFullYear) – Xotic750 Feb 14 '14 at 04:08
  • For years less than 1900, the value returned by getYear is less than 0. For example, if the year is 1800, getYear returns -100. I couldn't get the year to show 4 digits so did some youtube research and got that. It didn't make sense to me but it worked. – user3199950 Feb 14 '14 at 04:12
  • ok, getFullYear is much better I see...and makes complete sense – user3199950 Feb 14 '14 at 04:16
  • Also have a look at this answer http://stackoverflow.com/questions/21516995/create-another-formated-date-string-from-an-iso8601-timestamp/21518181#21518181 You may find some useful tips. There are also many more Q&A's here on SO that will help you, just do a search. – Xotic750 Feb 14 '14 at 04:21
  • If you have a specific question, then create a new question that asks for a single specific answer. See [help](http://stackoverflow.com/help) about asking questions. Be prepared to have questions closed if it looks like you are asking people to do your homework or the question has been asked before. – Xotic750 Feb 14 '14 at 04:23
  • You are welcome and good reading! :) – Xotic750 Feb 14 '14 at 04:27

1 Answers1

0

Try this as your function:

function customDateString(oneDate) {
    var theDay = dayNames[oneDate.getDay() + 1]
    var theMonth = monthNames[oneDate.getMonth() + 1]
    var theYear = oneDate.getFullYear()
    theYear += (theYear < 100) ? 1900 : 0
    return 'It is ' + new Date().timeNow() + ' on ' + theMonth + " " + oneDate.getDate() + ", " + theYear + '.  ';

}

With this prototype:

    Date.prototype.timeNow = function () {
     return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}

Demo

getFullYear returns a four-digit year, instead of just getYear which, for example, would return 14 in the year 2014.

Cilan
  • 13,101
  • 3
  • 34
  • 51