Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Date with greetings

  1. #1
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default Date with greetings

    Hey there, I am horrible with JavaScript but trying to learn it. Anyways I have some (horrible) javascript written where based on the computers time would display a greeting along with the time and change the background according to the time. This is what I have so far

    Code:
    var Digital=new Date();
    var hours=Digital.getHours();
    
    /* Live Date script from Dynamic Drive */
    
    var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
    var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    
    function getthedate(){
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
    year+=1900
    var day=mydate.getDay()
    var month=mydate.getMonth()
    var daym=mydate.getDate()
    if (daym<10)
    daym="0"+daym
    var hours=mydate.getHours()
    var minutes=mydate.getMinutes()
    var seconds=mydate.getSeconds()
    var dn="AM"
    if (hours>=12)
    dn="PM"
    if (hours>12){
    hours=hours-12
    }
    if (hours==0)
    hours=12
    if (minutes<=9)
    minutes="0"+minutes
    if (seconds<=9)
    seconds="0"+seconds
    
    /*End Live Date script */
    
    if (hours>=5&&hours<=11) {
    document.write('Good morning! It is now '+time+' on '+date+');}
    
                                      // I have no idea how to do variables. Basically print something like this:
                                      //
                                      // Good morning! It is now
                                      //
                                      // 9:12:32 A.M. 
                                      // Saturday, January 12th, 2014
                                      //  
                                      //
                                      // Then somehow have the background of the page a photo of a sunrise (lets call that photo sunrise.jpg)
    
    
    else if (hours==12) {
    document.write('Lunch time!');}
    
    else if (hours>=13&&hours<=17) {
    document.write('Good afternoon');}
    
    else if (hours>=18&&hours<=20) {
    document.write('Good evening');}
    
    else if (hours>=21&&hours<=11) {
    document.write('Its late.');}
    
    else {
    document.write('Hello night-owl!');}
    An inline div is a freak of the web and should be beaten until it becomes a span

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script type="text/javascript">
    <!--
    var Digital=new Date();
    var hours=Digital.getHours();
    
    /* Live Date script from Dynamic Drive */
    
    var dayarray=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var montharray=["January","February","March","April","May","June","July","August","September","October","November","December"];
    
    function getthedate(){
     var mydate=new Date(2014,9,7,7);
     var year=mydate.getFullYear();
     var day=mydate.getDay();
     var month=mydate.getMonth();
     var daym=mydate.getDate();
     if (daym<10)
      daym="0"+daym;
     var hours=mydate.getHours();
     var minutes=mydate.getMinutes();
     var seconds=mydate.getSeconds();
     var dn="AM";
     if (hours>=12)
      dn="PM";
     if (hours>12){
      hours=hours-1;2
     }
     if (hours==0)
      hours=12;
     if (minutes<10)
      minutes="0"+minutes;
     if (seconds<=9)
      seconds="0"+seconds;
    
     if (hours>=5&&hours<=11) {
      var date=mydate.getDate()+'';
      var d=date.charAt(0);
      document.write('Good morning! It is now <br><br>'+(hours+':'+minutes+':'+seconds+dn)+'<br>'+(dayarray[mydate.getDay()]+', '+montharray[mydate.getMonth()]+' '+date+(d=='1'?'st':d=='2'?'nd':d=='3'?'rd':'th')+' '+year));
    //  document.body.style.backgroundImage='url(http://www.vicsjavascripts.org/StdImages/1.gif)';
                                      // I have no idea how to do variables. Basically print something like this:
                                      //
                                      // Good morning! It is now
                                      //
                                      // 9:12:32 A.M.
                                      // Saturday, January 12th, 2014
                                      //
                                      //
                                      // Then somehow have the background of the page a photo of a sunrise (lets call that photo sunrise.jpg)
    
     }
    
     else if (hours==12) {
      document.write('Lunch time!');
     }
    
     else if (hours>=13&&hours<=17) {
      document.write('Good afternoon');
     }
    
     else if (hours>=18&&hours<=20) {
      document.write('Good evening');
     }
    
     else if (hours>=21&&hours<=11) {
      document.write('Its late.');
     }
    
     else {
      document.write('Hello night-owl!');
     }
    /*
    */
    
    }
    getthedate();
    //-->
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Why is the time stuck at 7 Am though? Heres a demo from Code Pen
    An inline div is a freak of the web and should be beaten until it becomes a span

  4. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    var mydate=new Date(2014,9,7,7);

    I do not know javascript very well at all either, but in the above line you will see that it reads September 7, 2014 at 7am. The problem is that the output is October.

    I'll leave the real answer to vwphillips or jscheuer1 though
    To choose the lesser of two evils is still to choose evil. My personal site

  5. #5
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by james438 View Post
    The problem is that the output is October.
    The problem is also that the date and time is preset, I can't figure out how to get the current date and time

    Edit:
    Quote Originally Posted by james438 View Post
    I'll leave the real answer to vwphillips or jscheuer1 though
    I think is jscheuer1 is busy at this post, among others.
    An inline div is a freak of the web and should be beaten until it becomes a span

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    There are errors, but less than before. As a reminder, my knowledge of javascript is near absolute beginner.

    Code:
    var Digital=new Date();
    var hours=Digital.getHours();
    
    /* Live Date script from Dynamic Drive */
    
    var dayarray=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var montharray=["January","February","March","April","May","June","July","August","September","October","November","December"];
     
    function getthedate(){
     var mydate=new Date();
     var year=mydate.getFullYear();
     var day=mydate.getDay();
     var month=mydate.getMonth();
     var daym=mydate.getDate();
     if (daym<10)
      daym="0"+daym;
     var hours=mydate.getHours();
     var minutes=mydate.getMinutes();
     var seconds=mydate.getSeconds();
     var dn="AM";
     if (hours>=12)
      dn="PM";
     if (hours>12){
      hours=hours-12;
     }
     if (hours===0)
      hours=12;
     if (minutes<10)
      minutes="0"+minutes;
     if (seconds<=9)
      seconds="0"+seconds;
    
     if (hours>=5&&hours<=11) {
      var date=mydate.getDate()+'';
      var d=date.charAt(0);
      document.write('Good morning! It is now <br><br>'+(hours+':'+minutes+':'+seconds+dn)+'<br>'+(dayarray[mydate.getDay()]+', '+montharray[mydate.getMonth()]+' '+date+(d=='1'?'st':d=='2'?'nd':d=='3'?'rd':'th')+' '+year));
    //  document.body.style.backgroundImage='url(http://www.vicsjavascripts.org/StdImages/1.gif)';
                                      // I have no idea how to do variables. Basically print something like this:
                                      //
                                      // Good morning! It is now
                                      //
                                      // 9:12:32 A.M.
                                      // Saturday, January 12th, 2014
                                      //
                                      //
                                      // Then somehow have the background of the page a photo of a sunrise (lets call that photo sunrise.jpg)
    
     }
    
     else if (hours==12) {
      document.write('Lunch Time! It is now <br><br>'+(hours+':'+minutes+':'+seconds+dn)+'<br>'+(dayarray[mydate.getDay()]+', '+montharray[mydate.getMonth()]+' '+date+(d=='1'?'st':d=='2'?'nd':d=='3'?'rd':'th')+' '+year));
     }
    
     else if (hours>=13&&hours<=17) {
      document.write('Good afternoon! It is now <br><br>'+(hours+':'+minutes+':'+seconds+dn)+'<br>'+(dayarray[mydate.getDay()]+', '+montharray[mydate.getMonth()]+' '+date+(d=='1'?'st':d=='2'?'nd':d=='3'?'rd':'th')+' '+year));
     }
    
     else if (hours>=18&&hours<=20) {
      document.write('Good evening! It is now <br><br>'+(hours+':'+minutes+':'+seconds+dn)+'<br>'+(dayarray[mydate.getDay()]+', '+montharray[mydate.getMonth()]+' '+date+(d=='1'?'st':d=='2'?'nd':d=='3'?'rd':'th')+' '+year));
     }
    
     else if (hours>=21&&hours<=11) {
      document.write('It is late! It is now <br><br>'+(hours+':'+minutes+':'+seconds+dn)+'<br>'+(dayarray[mydate.getDay()]+', '+montharray[mydate.getMonth()]+' '+date+(d=='1'?'st':d=='2'?'nd':d=='3'?'rd':'th')+' '+year));
     }
    
     else {
      document.write('Hello night owl! It is now <br><br>'+(hours+':'+minutes+':'+seconds+dn)+'<br>'+(dayarray[mydate.getDay()]+', '+montharray[mydate.getMonth()]+' '+date+(d=='1'?'st':d=='2'?'nd':d=='3'?'rd':'th')+' '+year));
     }
    /*
    */
    
    }
    getthedate();
    To choose the lesser of two evils is still to choose evil. My personal site

  7. #7
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by james438 View Post
    my knowledge of javascript is near absolute beginner.
    Same with me, I wish I could find someone on these forums who could help
    An inline div is a freak of the web and should be beaten until it becomes a span

  8. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Did the above not answer your question?
    To choose the lesser of two evils is still to choose evil. My personal site

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Let's see, this should work (does in moderate testing here). There might be a slight learning curve. I will explain the essentials, then - if there are any questions, please ask. First the code, please notice the highlighted part near the end:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Time Driven Events Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    // Time Driven Events Script (c)2014 John Davenport Scheuer
    // as first seen in http://www.dynamicdrive.com/forums/
    // username: jscheuer1 - This Notice Must Remain for Legal Use
    var timedriver = {
    	setrange: function(strt, end, msg, action){
    		strt = strt.toString(10).split(':');
    		end = end.toString(10).split(':');
    		if (!(this.hrs >= strt[0] && this.hrs <= end[0])) {return;}
    		if (this.hrs == strt[0] && strt[1] && strt[1] < this.mns) {return;}
    		if (this.hrs == end[0] && end[1] && this.mns >= end[1]) {return;}
    		this.setmsgel();
    		if(this.msgel){
    			this.msgel.innerHTML = msg.join(this.h + ':' + this.m + this.suffix);
    		}
    		action && action();
    	},
    	setmsgel: function(){
    		return this.msgel || (this.msgel = document.getElementById(this.elid));
    	},
    	now : new Date(),
    	hours: function(){return this.h = this.hrs = this.now.getHours();},
    	mins: function(){return this.m = this.mns = this.now.getMinutes();},
    	ampm: function(){
    		this.suffix = this.hrs > 11? 'pm' : 'am';
    		this.h = this.hrs > 11? this.hrs - 12 : this.hrs;
    		this.m = this.mns > 9? this.mns : '0' + this.mns;
    		return this.suffix;
    	},
    	init: function(){
    		this.hours();
    		this.mins();
    		this.ampm();
    		return this;
    	}
    };
    </script>
    </head>
    <body>
    <div id="timemsg"></div>
    <script type="text/javascript">
    var mytime = timedriver.init();
    mytime.elid = 'timemsg';
    mytime.setrange('0', '8', ["Good Morning! It's ", ", Up early, huh?"], function(){document.body.style.background = 'yellow';});
    mytime.setrange('20', '21', ["Good Evening! It's ", ", Nice night, huh?"], function(){document.body.style.background = 'lightblue';});
    mytime.setrange('22', '23:59', ["Good Night! It's ", ", Why are you still up?"], function(){document.body.style.background = 'blue';});
    </script>
    </body>
    </html>
    That (the highlighted) is what you will use (with modifications as desired) to accomplish your aims. Notice the red timemsg id for the div and how it's also used in the mytime.elid statement to tell the code the id of the element that you want the message to appear in.

    Also please notice that you need to first initialize the code to a variable (in this example I've used - mytime). After that, anything else you do will be that variable.whatever, ex:

    Code:
    mytime.setrange
    The real key here is that setrange command. It accepts a quoted start time in 24 hour notation. This can be in any of the normal formats, like 'h:mm', 'hh', 'hh:m', 'hh:mm', etc. Basically just hours, or hours and minutes separated by a colon. single or two digit hours may be used. same thing with the minutes (one or two digits as needed). Remember this is a string, so it must be quoted. This is when whatever you want to happen during this range of time begins. The next parameter is the end time. Same format as for start. It's when this range is no longer valid (if the current time is after or equal to end, this range will not execute). Next comes the message. It is an array with two members (before and after), ex:

    Code:
    ["Good Evening! It's ", ", Nice night, huh?"]
    You can have just a before:

    Code:
    ["Good Evening! It's "]
    or an empty before and just an after:

    Code:
    ["", ", Nice night, huh?"]
    What will be put in the 'middle' (location of the red comma, or at the end of a beginning only array) is the time in a format like:

    10:01pm

    Finally we have an action to perform. If there is none, nothing further will be done. If there is one, it must be a function, ex:

    Code:
    function(){document.body.style.background = 'blue';}
    If you want to use a background image for the body of the page, that would be like:

    Code:
    function(){document.body.style.background = 'url: (myimage.jpg)';}
    More complete instructions can be given, like positioning, repeat, etc. all according to the rules of css style and how they may be expressed in javascript. If you need further help with that - expressing just the sort of background - or anything else you might want in this function, just let me know.
    Last edited by jscheuer1; 10-10-2014 at 04:40 AM. Reason: add another setrange command for completeness
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Aug 2009
    Location
    utf-8
    Posts
    205
    Thanks
    4
    Thanked 7 Times in 7 Posts

    Default

    Quote Originally Posted by jscheuer1 View Post
    The real key here is that setrange command. It accepts a quoted start time in 24 hour notation. .
    How do I change it to 12 hour? As in 1:22 AM? Also what about for adding seconds and the date to the time string? My goal is to get something like this:

    Quote Originally Posted by FrickenTrevor View Post

    var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
    var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

    function getthedate(){
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
    year+=1900
    var day=mydate.getDay()
    var month=mydate.getMonth()
    var daym=mydate.getDate()
    if (daym<10)
    daym="0"+daym
    var hours=mydate.getHours()
    var minutes=mydate.getMinutes()
    var seconds=mydate.getSeconds()
    var dn="AM"
    if (hours>=12)
    dn="PM"
    if (hours>12){
    hours=hours-12
    }
    if (hours==0)
    hours=12
    if (minutes<=9)
    minutes="0"+minutes
    if (seconds<=9)
    seconds="0"+seconds

    if (hours>=5&&hours<=11) {
    document.write('Good morning! It is now '+time+' on '+date+');}

    // I have no idea how to do variables. Basically print something like this:
    //
    // Good morning! It is now
    //
    // 9:12:32 A.M.
    // Saturday, January 12th, 2014
    //
    //
    // Then somehow have the background of the page a photo of a sunrise (lets call that photo sunrise.jpg)



    else if (hours==12) {
    document.write('Lunch time!');}

    else if (hours>=13&&hours<=17) {
    document.write('Good afternoon');}

    else if (hours>=18&&hours<=20) {
    document.write('Good evening');}

    else if (hours>=21&&hours<=11) {
    document.write('Its late.');}

    else {
    document.write('Hello night-owl!');}

    [/CODE]
    An inline div is a freak of the web and should be beaten until it becomes a span

Similar Threads

  1. Jason's Date Input Calendar - Need Help to Display Empty Date Fields?
    By Dorgs in forum Dynamic Drive scripts help
    Replies: 9
    Last Post: 06-07-2013, 02:08 PM
  2. passing & recieving a date with mysql using Jason's Date Input Calendar
    By kkslider in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 02-19-2012, 04:30 PM
  3. Replies: 4
    Last Post: 10-29-2008, 05:40 AM
  4. Jason's Date Input Calendar onClick to change the showing date
    By williamstam in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 03-13-2008, 12:48 PM
  5. Replies: 1
    Last Post: 06-25-2007, 12:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •