JavaScript setUTCFullYear method
setUTCFullYear() method sets the full year for the provided date according to the Universal Coordinated Time (UTC). Following is the syntax for using setUTCFullYear() is as follows :
Syntax :
date_object.setUTCFullYear( year, month, day ); |
Where year is required parameter and month and day are optional. If we want to set only year then we can pass only one parameter year within method setUTCFullYear().
Description of example code:
To illustrate the use of setUTCFullYear() method we have created a simple HTML page into which we have three input text boxes to take three input values year , month and day. We also have created a button "Set UTC date" which calls the function setUTCFullYearFunc() on the button click. Code for function is as follows :
function setUTCFullYearFunc(){ var UTCYear = document.getElementById( 'UTCyearValue').value; var UTCMonth = document.getElementById( 'UTCmonthValue').value; var UTCDay = document.getElementById( 'UTCdayValue').value; var myDate = new Date(); alert("Before setting year is : "+myDate); myDate.setUTCFullYear(UTCYear,UTCMonth,UTCDay); alert("After setting year is : "+myDate); } |
In the above lines of code, first three lines of code specifies that we have taken the year, month and day values into three variables and then further these would be passed to the setUTCFullYear() method to set UTC( Universal Coordinated Time ) date. Here is the full code in HTML as follows :
<html> <head> <title>Set UTC full year example</title> <script> function setUTCFullYearFunc(){ var UTCYear = document.getElementById( 'UTCyearValue').value; var UTCMonth = document.getElementById( 'UTCmonthValue').value; var UTCDay = document.getElementById( 'UTCdayValue').value; var myDate = new Date(); alert("Before setting year is : "+myDate); myDate.setUTCFullYear(UTCYear,UTCMonth,UTCDay); alert("After setting year is : "+myDate); } </script> </head> <body> <div style="background: #ff9900; width:'100%';" align="center"> <font color="#0000ff" size="12pt"> <b>Set UTC Full Year </b> </font> </div> <p> </p> <div align="center"> <center> <table border="0" cellpadding="0" cellspacing="0" width="50%"> <tr> <td width="50%" align="left"> Insert any year(4-digit) to set </td> <td width="50%" align="left"> <input type="text" id="UTCyearValue" /> </td> </tr> <tr> <td width="50%" align="left"> Insert any number between 0 to 11 to set month </td> <td width="50%" align="left"> <input type="text" id="UTCmonthValue" /> </td> </tr> <tr> <td width="50%" align="left"> Insert any year(4-digit) to set </td> <td width="50%" align="left"> <input type="text" id="UTCdayValue" /> </td> </tr> <tr> <td width="50%" align="left"></td> <td width="50%" align="left"></td> </tr> <tr> <td width="100%" colspan="2" align="center"> <input type="button" onclick="setUTCFullYearFunc();" value="Set UTC date "> </td> </tr> </table> </center> </div> </body> </html> |
Output :
Suppose we want to set UTC full year value with the date 15 July 1947 then we will insert these values as follows and there after we will click on the button "Set UTC date".
It shows the current date before setting year in an alert message box.
You can see that it finally shows the set value.