Javascript date functions jquery

JavaScript Get Date Methods

In JavaScript, date objects are created with new Date() .

new Date() returns a date object with the current date and time.

Get the Current Time

Date Get Methods

Method Description
getFullYear() Get year as a four digit number (yyyy)
getMonth() Get month as a number (0-11)
getDate() Get day as a number (1-31)
getDay() Get weekday as a number (0-6)
getHours() Get hour (0-23)
getMinutes() Get minute (0-59)
getSeconds() Get second (0-59)
getMilliseconds() Get millisecond (0-999)
getTime() Get time (milliseconds since January 1, 1970)

Note 1

The get methods above return Local time.

Universal time (UTC) is documented at the bottom of this page.

Note 2

The get methods return information from existing date objects.

In a date object, the time is static. The «clock» is not «running».

The time in a date object is NOT the same as current time.

The getFullYear() Method

The getFullYear() method returns the year of a date as a four digit number:

Examples

Warning !

Old JavaScript code might use the non-standard method getYear().

getYear() is supposed to return a 2-digit year.

getYear() is deprecated. Do not use it!

The getMonth() Method

The getMonth() method returns the month of a date as a number (0-11).

Note

In JavaScript, January is month number 0, February is number 1, .

Finally, December is month number 11.

Examples

Note

You can use an array of names to return the month as a name:

Examples

const months = [«January», «February», «March», «April», «May», «June», «July», «August», «September», «October», «November», «December»];

const d = new Date(«2021-03-25»);
let month = months[d.getMonth()];

const months = [«January», «February», «March», «April», «May», «June», «July», «August», «September», «October», «November», «December»];

const d = new Date();
let month = months[d.getMonth()];

The getDate() Method

The getDate() method returns the day of a date as a number (1-31):

Examples

The getHours() Method

The getHours() method returns the hours of a date as a number (0-23):

Examples

The getMinutes() Method

The getMinutes() method returns the minutes of a date as a number (0-59):

Читайте также:  Java using dll files

Examples

The getSeconds() Method

The getSeconds() method returns the seconds of a date as a number (0-59):

Examples

The getMilliseconds() Method

The getMilliseconds() method returns the milliseconds of a date as a number (0-999):

Examples

The getDay() Method

The getDay() method returns the weekday of a date as a number (0-6).

Note

In JavaScript, the first day of the week (day 0) is Sunday.

Some countries in the world consider the first day of the week to be Monday.

Examples

Note

You can use an array of names, and getDay() to return weekday as a name:

Examples

const days = [«Sunday», «Monday», «Tuesday», «Wednesday», «Thursday», «Friday», «Saturday»];

const d = new Date(«2021-03-25»);
let day = days[d.getDay()];

const days = [«Sunday», «Monday», «Tuesday», «Wednesday», «Thursday», «Friday», «Saturday»];

const d = new Date();
let day = days[d.getDay()];

The getTime() Method

The getTime() method returns the number of milliseconds since January 1, 1970:

Examples

The Date.now() Method

Date.now() returns the number of milliseconds since January 1, 1970.

Examples

Calculate the number of years since 1970/01/01:

const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;

let years = Math.round(Date.now() / year);

Date.now() is a static method of the Date object.

You cannot use it on a date object like myDate.now() .

The syntax is always Date.now() .

UTC Date Get Methods

Method Same As Description
getUTCDate() getDate() Returns the UTC date
getUTCFullYear() getFullYear() Returns the UTC year
getUTCMonth() getMonth() Returns the UTC month
getUTCDay() getDay() Returns the UTC day
getUTCHours() getHours() Returns the UTC hour
getUTCMinutes() getMinutes() Returns the UTC minutes
getUTCSeconds() getSeconds() Returns the UTC seconds
getUTCMilliseconds() getMilliseconds() Returns the UTC milliseconds

UTC methods use UTC time (Coordinated Universal Time).

UTC time is the same as GMT (Greenwich Mean Time).

The difference between Local time and UTC time can be up to 24 hours.

The getTimezoneOffset() Method

The getTimezoneOffset() method returns the difference (in minutes) between local time an UTC time:

Example

Complete JavaScript Date Reference

For a complete Date reference, go to our:

The reference contains descriptions and examples of all Date properties and methods.

Источник

jQuery DATETIME Functions – Complete Listing

This is the only Date/Time jQuery functions you will ever need. It works better than any other date/time libraries out there and has minimal overhead, guaranteed speed and accuracy.

Includes functions to: get date, convert date, valid date, string to date, leap year, compare date, format date, timezone and heaps of others.

jQuery Date/Time Complete Listing

/*___ FILE: “JQUERY4U.datetime.js” ___*/
;(function($)
/**
* jQuery Date and time functions – Complete List
*/
var JQUERY4U = JQUERY4U || <>;
JQUERY4U.DATETIME =
/**
* Name of this class (used for error handling and/or debugging purposes)
* @type String
*/
name: ‘JQUERY4U.DATETIME’,

Читайте также:  Convert date value to string in java

init: function()
JQUERY4U.UTIL.handleErrors(this);
Date.prototype.JQUERY4UFormat = this.format;
>,

/**
* Return today’s date in dd/mm/yyyy format
* @returns Date in dd/mm/yyyy format
*/
todaysDate: function()
return this.futureDateDays(0);
>,

/**
* Return tomorrow’s date in dd/mm/yyyy format
* @returns Date in dd/mm/yyyy format
*/
tomorrowsDate: function()
return this.futureDateDays(1);
>,

/**
* Return date 7 days from now in dd/mm/yyyy format
* @returns Date in dd/mm/yyyy format
*/
weekFromToday: function()
return this.futureDateDays(7);
>,

/**
* Return the first day of the next month
* @returns Date in dd/mm/yyyy format
*/
firstDayNextMonth: function()
var today = new Date();
nextMonth = new Date(today.getFullYear(), today.getMonth() + 1, 1);
nextMonth.getDate() +’/’+ (nextMonth.getMonth() + 1) +’/’+ nextMonth.getFullYear();
return this.leadingZero(nextMonth.getDate()) +’/’+ this.leadingZero(nextMonth.getMonth() + 1) +’/’+ nextMonth.getFullYear();
>,

/**
* Returns x number of dates date in the future in dd/mm/yyyy format
* @param days Number of days into the future
* @returns Date in dd/mm/yyyy format
*/
futureDateDays: function(days)
var futureDate = new Date();
futureDate.setDate(futureDate.getDate() + days);
return this.leadingZero(futureDate.getDate()) +’/’+ this.leadingZero(futureDate.getMonth() + 1) +’/’+ this.leadingZero(futureDate .getFullYear());
>,

/**
* Return the current time in HHMM format
* @returns Time in HHMM (e.g. 23:12) format
*/
timeHHMM: function()
var today = new Date();
return this.leadingZero(today.getHours()) + this.leadingZero(today.getMinutes());
>,

/**
* Return the current time in HHMMSS format
* @returns Time in HHMMSS (e.g. 23:12:33) format
*/
timeHHMMSS: function()
var today = new Date();
return this.leadingZero(today.getHours()) +’:’+ this.leadingZero(today.getMinutes()) +’:’+ this.leadingZero(today.getSeconds());
>,

/**
* Takes a date string in Australian format and returns date string in US format
* @param dateStr Date in dd/mm/yyyy format
* @param [separator=”-“] separator character in return date string
* @returns date in mm/dd/yyyy format
*/
convertUSFormat: function(dateStr, separator)
var separator = (typeof(separator) == ‘undefined’) ? ‘-‘ : separator;
var re = new RegExp(‘(3)/(9)/(6)’, ‘m’);
var matches = re.exec(dateStr);
return matches[2] + separator + matches[1] + separator + matches[3];
>,

/**
* Convert date in mm/dd/yyyy format and return in dd-mm-yyyy format (depending upon separator)
* @param dateStr Date in mm/dd/yyyy format
* @param [separator=”-“] Separator character in return date string
* @returns Date in mm-dd-yyyy format (presuming “-” is separator character)
*/
convertUStoAUSDate: function(dateStr, separator)
var separator = (typeof(separator) == ‘undefined’) ? ‘-‘ : separator;
var re = new RegExp(‘(9)/(1)/(8)’, ‘m’);
var matches = re.exec(dateStr);
return matches[2] + separator + matches[1] + separator + matches[3];
>,

/**
* Return whether the supplied date components form the expected date
* @param year
* @param month
* @param day
* @returns True if the date components match the date values in Date object
*/
isValidDate: function(year, month, day)
var dt = new Date(parseInt(year, 10), parseInt(month, 10)-1, parseInt(day, 10));
if(dt.getDate() != parseInt(day, 10) || dt.getMonth() != (parseInt(month, 10)-1) || dt.getFullYear() != parseInt(year, 10))
return false;
>

Читайте также:  Combine css files online

/**
* Takes a date object and returns in yyyymmdd format
* @param dateObj
* @returns Date in yyyymmdd format
*/
dateToYYYYMMDD: function(dateObj)
return (dateObj.getFullYear() + this.leadingZero(dateObj.getMonth() + 1) + this.leadingZero(dateObj.getDate())).toString();
>,

/**
* Takes a date object and returns in ddmmyyyy format
* @param dateObj
* @returns Date in ddmmyyyy format
*/
dateToDDMMYYYY: function(dateObj)
return (this.leadingZero(dateObj.getDate()) + this.leadingZero(dateObj.getMonth() + 1) + dateObj.getFullYear()).toString();
>,

/**
* Takes a date string in dd/mm/yyyy format
* @param dateString Date in dd/mm/yyyy format
* @returns Returns false if date sring is invalid
*/
stringToDate: function(dateString)
try
var matches = dateString.match(/(9)/(8)/(4)/);
if(this.isValidDate(matches[3], matches[2], matches[1]) === false)
return false;
>

return new Date(matches[3], parseInt(matches[2], 10)-1, parseInt(matches[1], 10));
>
catch(e)
return false;
>
>,

/**
* Adds leading zero if passed value is single digit
* @param val
* @returns
*/
leadingZero: function(val)
var str = val.toString();
if(str.length == 1)
str = ‘0’ + str;
>

/**
* Checks if return date is equal or after departure date
* @param departureDate
* @param returnDate
* @returns
*/
isDepartureReturnDateValid: function(departureDate, returnDate)
var dep = this.stringToDate(departureDate);
var ret = this.stringToDate(returnDate);
if(dep > ret)
return false;
>

/**
* Detect whether the year supplied is a leap year
* @param year
* @returns
*/
isLeapYear: function(year)
year = parseInt(year, 10);
if(year % 4 == 0)
if(year % 100 != 0)
return true;
>
else
if(year % 400 == 0)
return true;
>
else
return false;
>
>
>
return false;
>,

compareDates: function(from, to)
var dateResult = to.getTime() – from.getTime();
var dateObj = <>;
dateObj.weeks = Math.round(dateResult/(1000 * 60 * 60 * 24 * 7));
dateObj.days = Math.ceil(dateResult/(1000 * 60 * 60 * 24));
dateObj.hours = Math.ceil(dateResult/(1000 * 60 * 60));
dateObj.minutes = Math.ceil(dateResult/(1000 * 60));
dateObj.seconds = Math.ceil(dateResult/(1000));
dateObj.milliseconds = dateResult;
return dateObj;
>,

compareDatesDDMMYYYY: function(from, to)
from = from.split(‘/’);
from = new Date(from[2], from[1], from[0]);
to = to.split(‘/’);
to = new Date(to[2], to[1], to[0]);
return this.compareDates(from, to);
>,

/**
* Allow nice formatting of dates like PHP’s Date function
* Derived from code written by Jac Wright at http://jacwright.com/projects/javascript/date_format
* @param date JavaScript date object
* @param format Date format string
* @returns
*/
format: function()
var date,
format,
args = [].slice.call(arguments),
returnStr = ”,
curChar = ”,
months = [‘January’, ‘February’, ‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’, ‘October’, ‘November’, ‘December’],
days = [‘Sunday’, ‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’],
methods =
// Day
d: function() < return (date.getDate()

Share This Article

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

Источник

Оцените статью