|
getTimezoneOffset()This method returns the time zone offset in minutes for the current locale. The time zone offset is the difference between local time and Greenwich Mean Time (GMT). Daylight savings time prevents this value from being a constant. The returned value is an integer representing the difference in minutes. The following script shows how to use the user’s time zone offset to figure out where he or she lives:
if (confirm("Are you in the United States?")) { var now = new Date() var curOffset = now.getTimezoneOffset() curOffset /= 60 // convert from minutes to hours var zone = "" var prep = "" if (curOffset == 8) { zone = "west coast" prep = "on" } else if (curOffset == 7) { zone = "mid - west" prep = "in" } else if (curOffset == 6) { zone = "mid - east" prep = "in" } else { zone = "east coast" prep = "on" } alert("I think you live " + prep + " the " + zone + "!") } else alert("Sorry, this script is intended for U.S. residents only")
The script starts by asking the user if he or she lives in the United States. If not, a message is displayed. Otherwise, the following command block is executed. The area in the United States is determined according to the difference in hours between the local time zone and the GMT. The preceding preposition (e.g., “in”, “on”) is determined as well in the if - else construct. JavaScript then builds an appropriate message based on the current location of the user as well as the proper preposition. getTime()The getTime() method returns the number of milliseconds since January 1, 1970 00:00:00. Here is an example that calculates the number of seconds passed since the beginning of the century:
var now = new Date() var milliseconds70 = now.getTime() var seconds70 = milliseconds70 / 1000 var seconds00 = 70 * 365 * 24 * 60 * 60 // years * days * hours * minutes * seconds // leap years and additional seconds not counted var totalSeconds = seconds00 + seconds70 document.write("Approximately " + totalSeconds + " seconds have passed in the 20th century!")
seconds70 holds the number of seconds since 1970. seconds00 holds the number of seconds from 1900 to 1970 (approximately). set MethodssetYear()This method sets the year attribute of a given Date instance. The following example computes the day of the current date last year:
var now = new Date() var year = now.getYear() now.setYear(year + 1) ar = new Array(7) ar[0] = "Sunday" ar[1] = "Monday" ar[2] = "Tuesday" ar[3] = "Wednesday" ar[4] = "Thursday" ar[5] = "Friday" ar[6] = "Saturday" document.write("Last year, the current day was " + ar[now.getDay()])
At first, an instance of the current date is created, and the current year is assigned to the variable year. The year attribute of the instance, now, is set to one year behind. The day attribute is then extracted from the modified instance, and a message is built based on that day, transformed to a string (via the array). setMonth()Sets the month attribute of a given instance of the Date object. The following script sets the month attribute of the current date to May:
var now = new Date() now.setMonth(4)
setDate()This method sets the date attribute of a given instance of the Date object. The following script prints the day on which the first day of the month occurred:
var now = new Date() now.setDate(1) ar = new Array(7) ar[0] = "Sunday" ar[1] = "Monday" ar[2] = "Tuesday" ar[3] = "Wednesday" ar[4] = "Thursday" ar[5] = "Friday" ar[6] = "Saturday" document.write("The first day of the month occurred on " + ar[now.getDay()])
setHours()This method sets the hour attribute of a given instance of the Date object. Here is an example:
var obj = new Date("December 4, 1995 18:50:59") // JS press release obj.setHours(obj.getHours() – 2) alert(obj.getHours()) // 16
setMinutes()This method sets the minutes of a given date instance. Here is a simple example:
var obj = new Date("December 4, 1995 18:50:59") // JS press release obj.setMinutes(obj.getMinutes() – 1) alert(obj.getMinutes()) // 49
setSeconds()The setSeconds method sets the seconds of a given instance of the Date object type. The following example demonstrates its usage:
var obj = new Date("December 4, 1995 18:50:59") // JS press release obj.setSeconds(obj.getSeconds() – 9) alert(obj.getSeconds()) // 50
setTime()This method sets the number of milliseconds since January 1, 1970 00:00:00. It actually modifies all fields of its calling object. Here is an example:
var obj = new Date() obj.setTime(867999600000) var date = obj.getDate() var month = obj.getMonth() if (date < 10) var lastDigit = date else var lastDigit = date % 10 var exp = "" // determine suffix if (lastDigit == 1) suf = "st" else if (lastDigit == 2) suf = "nd" else if (lastDigit == 3) suf = "rd" else suf = "th" // array for name of month var ar = new Array(12) ar[0] = "January" ar[1] = "February" ar[2] = "March" ar[3] = "April" ar[4] = "May" ar[5] = "June" ar[6] = "July" ar[7] = "August" ar[8] = "September" ar[9] = "October" ar[10] = "November" ar[11] = "December" var text = ar[month] + " " +date + suf alert(text) // July 4th (setTime modifies the entire instance)
to MethodstoGMTString()This method converts a date to a string, using the Internet GMT conventions. The conversion is done according to the operating system’s time zone offset and returns a string value that is similar to the following form:
Tue, 30 Jul 1996 01:03:46 GMT
The exact format depends on the platform. Here is a simple example:
var now = new Date() var ar1 = now.toGMTString().split(" ") document.write("The current time in Greenwich is " + ar1[4])
A sample output of this script segment is: The current time in Greenwich is 01:08:21
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |