Understanding some of the basic built-in javascript date methods
This blog gives a brief overview of the basic javascript methods that are related to dates.
1. Date()
This method returns current date with time (local date & time).
<script type="text/javascript">
//Date()
document.write(Date()); //Output will be current date
</script>
2. getDate()
This method returns current day from an given date.
<script type="text/javascript">
//getDate()
var date = new Date();
document.write(date.getDate()); //Output will be current day
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getDate()); //Output will be 21
</script>
3. getDay()
This method returns the day of a week. Return will be an integer corresponding to particular day. (0 for Sunday, 1 for Monday, etc..)
<script type="text/javascript">
//getDay()
var date = new Date();
document.write(date.getDay()); //Output will be index of current day of week
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getDay()); //Output will be 5
</script>
4. getFullYear()
This method returns year from a provided date.
<script type="text/javascript">
//getFullYear()
var date = new Date();
document.write(date.getFullYear()); //Output will be current year
var date = new Date("Fri Oct 23 2015 14:36:25");
document.write(date.getFullYear()); //Output will be 2015
</script>
5. getMonth()
This method returns month as integer (0 for January, 1 for February, etc..) from a provided date.
<script type="text/javascript">
//getMonth()
var date = new Date();
document.write(date.getMonth()); //Output will be current month
var date = new Date("Fri Oct 23 2016 14:36:25");
document.write(date.getMonth()); //Output will be 10
</script>
6. getHours()
This method returns hour value from given date.
<script type="text/javascript">
//getHours()
var date = new Date();
document.write(date.getHours()); //Output will be current hour
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getHours()); //Output will be 14
</script>
7. getMinutes()
This method returns minute value from given date.
<script type="text/javascript">
//getMinutes()
var date = new Date();
document.write(date.getMinutes()); //Output will be current minute
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getMinutes()); //Output will be 36
</script>
8. getSeconds()
This method returns seconds value from given date.
<script type="text/javascript">
//getSeconds()
var date = new Date();
document.write(date.getSeconds()); //Output will be current seconds value
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getSeconds()); //Output will be 25
</script>
Hope this will be helpful!
This blog gives a brief overview of the basic javascript methods that are related to dates.
1. Date()
This method returns current date with time (local date & time).
<script type="text/javascript">
//Date()
document.write(Date()); //Output will be current date
</script>
2. getDate()
This method returns current day from an given date.
<script type="text/javascript">
//getDate()
var date = new Date();
document.write(date.getDate()); //Output will be current day
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getDate()); //Output will be 21
</script>
3. getDay()
This method returns the day of a week. Return will be an integer corresponding to particular day. (0 for Sunday, 1 for Monday, etc..)
<script type="text/javascript">
//getDay()
var date = new Date();
document.write(date.getDay()); //Output will be index of current day of week
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getDay()); //Output will be 5
</script>
4. getFullYear()
This method returns year from a provided date.
<script type="text/javascript">
//getFullYear()
var date = new Date();
document.write(date.getFullYear()); //Output will be current year
var date = new Date("Fri Oct 23 2015 14:36:25");
document.write(date.getFullYear()); //Output will be 2015
</script>
5. getMonth()
This method returns month as integer (0 for January, 1 for February, etc..) from a provided date.
<script type="text/javascript">
//getMonth()
var date = new Date();
document.write(date.getMonth()); //Output will be current month
var date = new Date("Fri Oct 23 2016 14:36:25");
document.write(date.getMonth()); //Output will be 10
</script>
6. getHours()
This method returns hour value from given date.
<script type="text/javascript">
//getHours()
var date = new Date();
document.write(date.getHours()); //Output will be current hour
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getHours()); //Output will be 14
</script>
7. getMinutes()
This method returns minute value from given date.
<script type="text/javascript">
//getMinutes()
var date = new Date();
document.write(date.getMinutes()); //Output will be current minute
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getMinutes()); //Output will be 36
</script>
8. getSeconds()
This method returns seconds value from given date.
<script type="text/javascript">
//getSeconds()
var date = new Date();
document.write(date.getSeconds()); //Output will be current seconds value
var date = new Date("Fri Oct 21 2016 14:36:25");
document.write(date.getSeconds()); //Output will be 25
</script>
Hope this will be helpful!
No comments:
Post a Comment