JavaScript tutorial-World Time


Date object

World Time value

The current date you have seen so far may different from one country to another based on the different places of those country. For example, the time 0:00(mid night) is displayed in London, but in Phnom Penh the time is 7:00. What we need is to have a standard time or world time to represent the current movement in time. To work with a standard time(called UTC-Coordinated Universal Time or GMT-Greenwich Mean Time), JavaScript provides methods similar to the date and time methods to have learnt.

Get and set world date methods:

getUTCFullYear()
getUTCMonth()
getUTCDate()
getUTCDay()

setUTCFullYear()
setUTCMonth()
setUTCDate()

Get and set world time methods:

getUTCHours()
getUTCMinutes()
getUTCSeconds()
getUTCMilliseconds()

setUTCHours()
setUTCMinutes()
setUTCSeconds()
setUTCMilliseconds()

Other three important methods are toUTCString(), toLocaleTime(), and getTimezoneOffset() methods. The first two methods display the current date and time based on UTC and Local time and latter gets the difference in minutes between the UTC time and the Local time.

Example:

<html>
<head>
<title>UTC Time</title>
<script language="JavaScript" type="text/javascript">
var curdate = new Date();
document.write("UTC time:"+curdate .toUTCString() +"<br>");
document.write("Local time:"+curdate .toLocaleString()+"<br>");
document.write("Difference:"+curdate .getTimezoneOffset()+" minutes") ;
</script>

</head>
<body>

</body>
</html>




Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.