-1

So the problem is to Display the current date but not the time. The date must be synced with the computer's time. ,and a pop up display message should say Good Morning if the time is AM and Good Evening if the time PM. I can do the pop up message but i don't know how to sync it to the time. Please i need help. thanks.

4 Answers4

1
var d    = new Date(),
    pm   = d.getHours() > 12,
    date = d.getDate() + '-' + (d.getMonth()+1) + '-' + d.getFullYear();

alert('Good ' + (pm ? 'evening' : 'morning') + ', it\'s ' + date);

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
1

I recommend using the moment.js library. It's excellent for formatting dates, and also has international language support.

For example, this will give you the current date:

moment().format('MMMM Do YYYY');

And this will give you 'AM' or 'PM':

moment().format("A");
linstantnoodles
  • 4,350
  • 1
  • 22
  • 24
0

You can access all of these properties individually using the Date object in JavaScript. Here's a quick example using JavaScript to display the date in the dateMonthYear div. jsFiddle example here.

Also there is a great example here that explains why the + 1 needs to be added to currentdate.getMonth(), and also how to use the protoype constructor for the Date object to create a new method of the Date object for date and time with your particular formatting.

HTML

<div id="dateMonthYear"></div>

JS

var currentdate = new Date();
var dateMonthYear = currentdate.getDate() + "/" + (currentdate.getMonth() + 1) + "/" + currentdate.getFullYear();
document.getElementById("dateMonthYear").innerHTML= dateMonthYear;
if (currentdate.getHours() < 12) {
    alertMessage = "Good Morning."
} else {
    alertMessage = "Good Evening."
}
alert(alertMessage);
Community
  • 1
  • 1
Space Age Crystal
  • 394
  • 1
  • 2
  • 13
-1
function funampm()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
if(h>11)
{
    alert("Good Evening");
}
else {
    alert("Good Morning");
}
}
Indranil.Bharambe
  • 1,462
  • 3
  • 14
  • 25