Getting Dates and Parts of Dates in JavaScript

Getting Dates and Parts of Dates in JavaScript
In a recent article we looked at the the JavaScript date object, in general. Here we look at all the methods for getting different parts of a date and time according to the local time. Local time is whatever time and timezone the computer the user is using is set to.

For these examples, date is set using var now=new Date() when local time was Friday, July 06, 2007 5:54:14 PM on a computer set to (US) Mountain Standard Time. If you have JavaScript available in your browser, you can see a live example here.
getMonth()
a number representing the month, 0 for January, 1 for February, ... You can use this as a key to an array of month names or add one to the number to get a human readable month
EXAMPLE: now.getMonth() RESULT: 6
getDate()
the day of the month as a number from 1 to 31
EXAMPLE: now.getDate() RESULT: 6
getDay()
the day of the week as a number from 0 to 6 where 0 represents Sunday and 6 represents Saturday
EXAMPLE: now.getDay() RESULT: 5
getFullYear()
returns the four digit year
EXAMPLE: now.getFullYear() RESULT: 2007
getYear()
returns the two digit year OR the four digit year minus 1900, you should probably avoid using this in favor of getFullYear()
EXAMPLE: now.getYear() RESULT: 07 OR 107 OR even 7
getHours()
hour of the day on a 24 hour clock (0 to 23)
EXAMPLE: now.getHours() RESULT: 17
getMinutes()
minutes after the hour (0 to 59)
EXAMPLE: now.getMinutes() RESULT: 54
getSeconds()
seconds of the minute (0 to 59)
EXAMPLE: now.getSeconds() RESULT: 14
getMilliseconds()
millionths of a second (0 to 999)
EXAMPLE: now.getMilliseconds() RESULT: 937
getTimezoneOffset()
Offset from UTC time in minutes
EXAMPLE: now.getTimezoneOffset() RESULT: 420
getTime()
current time in native JavaScript format, Milliseconds since January 1st, 1970. This is often called UNIX time, since the UNIX operating system popularized this time format.
EXAMPLE: now.getTime() RESULT: 1183769654937


This site needs an editor - click to learn more!


You Should Also Read:
Introduction To The JavaScript Date Object
Javascript Resources
JavaScript/Java Newsletter

RSS
Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map





Content copyright © 2023 by Julie L Baumler. All rights reserved.
This content was written by Julie L Baumler. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.