Skip navigation

Lu's Notes

down to bottom of page

Find & Display the Time or Date

Dates and Time are sometimes calculated by the number of milliseconds since: January 1, 1970.
Milliseconds per day is calculated by  (1000*60*60*24)  or 
(milliseconds/second*seconds/minute*minutes/hour*hours/day)

JavaScript TIME:                    Example

<SCRIPT language="JavaScript">
<!--
        var now  = new Date ();
        var hour = now.getHours ();
        var minu = now.getMinutes ();
                if (minu < 10) {minu = "0" + minu}
        var mon  = now.getMonth();
        var mday = now.getDate();
        var wday = now.getDay();
        var dayname;
        var thisYr = now.getYear();
                   if (thisYr < 1900) {thisYr = thisYr + 1900}
        var nextYr = thisYr + 1
        var xmas = new Date(thisYr,11,25);
                   if (xmas.getTime() < now.getTime()) {xmas.setYear(nextYr)}

        function dayTodays(inTime)
                   {
                   return (Math.floor(inTime.getTime()/(1000*60*60*24)))
                   }
        function daysTill(inDate)
                   {
                   return dayTodays(inDate) - dayTodays(now)
                   }
        if (mon == 0) monthname = "January";
        if (mon == 1) monthname = "February";
        if (mon == 2) monthname = "March";
        if (mon == 3) monthname = "April";
        if (mon == 4) monthname = "May";
        if (mon == 5) monthname = "June";
        if (mon == 6) monthname = "July";
        if (mon == 7) monthname = "August";
        if (mon == 8) monthname = "September";
        if (mon == 9) monthname = "October";
        if (mon == 10) monthname = "November";
        if (mon == 11) monthname = "December";

        if (wday == 0) dayname = "Sunday";
        if (wday == 1) dayname = "Monday";
        if (wday == 2) dayname = "Tuesday";
        if (wday == 3) dayname = "Wednesday";
        if (wday == 4) dayname = "Thursday";
        if (wday == 5) dayname = "Friday";
        if (wday == 6) dayname = "Saturday";

        var time    = "The time on your computer is " + hour + ":" + minu +  ".  ";
        var thisday = "Today is " + dayname + ", " + monthname + " " + mday + ", " + thisYr + ".  ";
        var tilxmas = "We must wait " +  daysTill(xmas) + " days for Christmas to arrive.";





        if (hour > 5 && hour < 8)
        {
        document.bgColor = "#F5C0DF";
        document.fgColor = "#FF0000";
        document.write("<p align=right><img src=funimages/merlin01.gif alt='picture of Merlin walking'></p>"); 
        document.write("<H2 align='center'>Good Morning, World!</H2>");
        document.write("<p align='center'>" + time + "<br>" + thisday + "<br>" + tilxmas );
        document.write("<p align='center'>Shouldn't you be getting ready for work?</p>");
        }
        else if (hour > 7 && hour < 12)
        {
        document.bgColor = "#CCFFFF";
        document.fgColor = "#0000FF";
        document.write("<p align=left><img src=funimages/woody-buzz01.gif alt='picture of Woody & Buzz
						 					      from Toy Story'></p>");   
        document.write("<H2 align='center'>Good Morning, World!</H2>");
        document.write("<p align='center'>" + time + "<br>" + thisday + "<br>" + tilxmas );
        document.write("<p align='center'>No web surfing during business hours!</p>"); 
        }
        else if (hour > 11 && hour < 17)
        {
        document.bgColor = "#FFFFFF";
        document.fgColor = "#F15B01";
        document.write("<H2 align='center'>Good Afternoon, World!</H2>");
        document.write("<p align='center'>" + time + "<br>" + thisday + "<br>"  + tilxmas );
        document.write("<p align='center'>No web surfing during business hours!</p>"); $
        document.write("<p align=right><img src=funimages/mushu02.gif alt='picture of dragon ringing a gong'></p>"); 
        }
        else if (hour > 16 && hour < 22)
        {
        document.bgColor = "#D6B9F0";
        document.fgColor = "#8936D3";
        document.write("<p align=right><img src=funimages/cheshire01.gif alt='picture of Cheshire Cat'></p>");
        document.write("<H2 align='center'>Good Evening, World!</H2>");
        document.write("<p align='center'>" + time + "<br>" + thisday + "<br>"  + tilxmas );
        document.write("<p align='center'>Time to relax? Or working a little late?</p>");
        }
        else
        {
        document.bgColor = "#000000";
        document.fgColor = "#FFFFFF";
        document.write("<p align=left><img src=funimages/merlin02.gif alt='picture of Merlin waving his wand'></p>");
        document.write("<H2 align='center'>Good Night, World!</H2>");
        document.write("<p align='center'>" + time + "<br>" + thisday + "<br>"  + tilxmas );
        document.write("<p align='center'>You are either up very early or are staying up way too late!</p>");
        }
//-->
</SCRIPT>

PERL Time....               CGI example not working on this server...

&Parse_Form; (input from previous page)

$selectMonth  = $formdata{selectMonth};
$selectDay    = $formdata{selectDay};
$selectYear   = $formdata{selectYear};
$selectSec    =  0;
$selectMin    =  0;
$selectHour   =  2;

use Time::Local;
$convertDate  = timelocal($selectSec, $selectMin, $selectHour, $selectDay, $selectMonth,
			        $selectYear);
$selectedDate = localtime($convertDate);

# Print the date
($mday, $month, $year, $wday) = (localtime($convertDate)) [3, 4, 5, 6];
$valueWday = $wday;
# Convert the year
$year += 1900;

@monthname = qw(January February March April May June July August September October
		     November December);  
@dayname   = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

print ”($dayname[$wday] $monthname[$month] $mday, $year)"; 
***will appear as: Monday January 1, 2000 

# Print out current date
sub get_current_date
        {
        ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time);
 
        @dayname   = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
        @monthname = (January, February, March, April, May, June, July, August, September,
			     October, November, December); 
        $year     +=  1900;
        $sec       =  sprintf("%02d", $sec);
        $min       =  sprintf("%02d", $min);
        $hour      =  sprintf("%02d", $hour);
        }

print ”($dayname[$wday] $monthname[$month] $mday, $year)"; 
***will appear as: Monday January 1, 2000 

ASP Date....

This .asp code runs on the server and displays the Date on the server at the time of the request.
<%=month(date)%>/<%=day(date)%>/<%=year(date)%> will appear as : 01/01/2001
<%= formatdatetime((now),vblongdate) %> will appear as : Monday, January 01, 2001
<%Date%>, <%Time%> will appear as : 01/01/2001, 01:01:00 AM
	
SQL Dates....

(MONTH(table_name.field_name)), (DAY(table_name.field_name)), (YEAR(table_name.field_name))
(HOUR(table_name.field_name)), (MINUTE(table_name.field_name)), (SECOND(table_name.field_name))
CURRENT_DATE, CURRENT_TIME, CURRENT_TIMESTAMP

Java Dates....

ValueObject:
import java.sql.Date;
public class SomeValueObject 
{
    private Date someDate;
		
    public Date getSomeDate()
    {
        return someDate;
    }
    public void setSomeDate(Date newSomeDate)
    {
        someDate = newSomeDate;
    }
}	

Formbean:
import java.sql.Date;  or  import java.util.Date;
import java.text.SimpleDateFormat;
import gov.ca.doj.hdc.agenttransfer.persistence.SomeValueObject;
public class SomeForm extends ActionForm
{
    private SimpleDateFormat formatted = new SimpleDateFormat("MM/dd/yyyy hh:mm");
    private SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    private Date today = new Date();
	
    public Date getToday()
    {
        return today;
    }
    public void setToday(Date newToday)
    {
        today = newToday;
    }
    public String getTodayFormatted()
    {
        return formatted.format(getToday());
    }
		
    system.out.println(this.getTodayFormatted());
	
    public Date getSomeDate()
    {
        return someVO.getSomeDate();
    }
    public void setSomeDate(Date newSomeDate)
    {
        someVO.setSomeDate(newSomeDate);
    }
    public String getSomeDateFormatted()
    {
        return formatter.format(getSomeDate());
    }
}

up to top of page   Return to Top of Page   up to top of page up to top of page