JavaScript: Date Cheat Sheet

CHEAT SHEET BOOKMARK

INTRO
Declaring/Creating Date
GET FUNCTIONS
SET FUNCTIONS
Date Convertions

Most of the time when we are saving data to our databases, one of the most types we always use is date types. So as a developer we should atle…


This content originally appeared on DEV Community and was authored by Jenuel Oras Ganawed

CHEAT SHEET BOOKMARK

Most of the time when we are saving data to our databases, one of the most types we always use is date types. So as a developer we should atleast know how we can manipulate dates. So in this articles I am showing how you can manipulate dates in JavaScript.

For more like this hit follow, and if you want to show support by donating, click this link: BUYMECOFFEE

Declaring Dates

// NEW DATE OBJECT
    // new Date() - will create a new Date Object in a current date
    let date = new Date();
    console.log(date); //Tue Feb 16 2021 12:04:01 GMT+0800 (Philippine Standard Time)

    let date2 = date2 = new Date('August 19, 1975 23:15:30 GMT-02:00'); // you can pass a date inside a parameter

// DATE NOW FUNCTION
    //Date.now - Returns the numeric value corresponding to the current time
    console.log(Date.now()); //1613449122179 

// UTC DATE
    const utcDate1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));
    const utcDate2 = new Date(Date.UTC(0, 0, 0, 0, 0, 0));

    console.log(utcDate1.toUTCString()); // expected output: Fri, 02 Feb 1996 03:04:05 GMT
    console.log(utcDate2.toUTCString()); // expected output: Sun, 31 Dec 1899 00:00:00 GMT

Enter fullscreen mode Exit fullscreen mode

Get Functions

To Get specific data in a date you can check this sample bellow code snippet.

let date = new Date();

// Get Current Date
let dt= date.getDate(); // 16

// Get Day
let day = date.getDay(); // 1 - Monday

//Get Year
let year = date.getFullYear(); // 2021

//Get Hours
let hours = date.getHours(); // 14

// Get Minutes
let minute = date.getMinutes(); // 48

// Get Seconds
let sec = date.getSeconds(); // 29

// Get Time - method returns the number of milliseconds
    // NOTE: _getTime() always uses UTC for time representation.
    // For example, a client browser in one timezone,
    // getTime() will be the same as a client browser in any other timezone._
let time = date.getTime(); // 14254782000

// Get TimeZone offset - method returns the time zone difference, in minutes, from current locale (host system settings) to UTC.
let time_zone_offset = date.getTimezoneOffset(); //-480

// ___________________
// Getting UTC DATES
// ____________________
const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');
const date2 = new Date('August 19, 1975 23:15:30 GMT-11:00');

// Get UTC date
console.log(date1.getUTCDate()); // expected output: 19
console.log(date2.getUTCDate()); // expected output: 20

// get UTC day
    // Tuesday
    console.log(date1.getUTCDay()); // expected output: 2
    // Wednesday
    console.log(date2.getUTCDay()); // expected output: 3

// get UTC Year
console.log(date1.getUTCFullYear()); // expected output: 1975
console.log(date2.getUTCFullYear()); // expected output: 1976

// get UTC Hours
console.log(date1.getUTCHours()); // expected output: 12
console.log(date2.getUTCHours()); // expected output: 10

// Get UTC Miliseconds
const exampleDate = new Date('2018-01-02T03:04:05.678Z'); // 2 January 2018, 03:04:05.678 (UTC)
console.log(exampleDate.getUTCMilliseconds()); // expected output: 678

// GET UTC Minutes
    const date1 = new Date('1 January 2000 03:15:30 GMT+07:00');
    const date2 = new Date('1 January 2000 03:15:30 GMT+03:30');
    console.log(date1.getUTCMinutes()); // 31 Dec 1999 20:15:30 GMT// expected output: 15
    console.log(date2.getUTCMinutes()); // 31 Dec 1999 23:45:30 GMT// expected output: 45

// GET UTC MONTHS
    const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');
    const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00');

    // December
    console.log(date1.getUTCMonth());// expected output: 11
    // January
    console.log(date2.getUTCMonth());// expected output: 0

// GET UTC SECONS
const moonLanding = new Date('July 20, 1969, 20:18:04 UTC');
console.log(moonLanding.getUTCSeconds()); // expected output: 4


Enter fullscreen mode Exit fullscreen mode

SET FUNCTIONS

// SET DATE
    const event = new Date('August 19, 1975 23:15:30');

    event.setDate(24);
    console.log(event.getDate()); // expected output: 24

    event.setDate(32); // Only 31 days in August!
    console.log(event.getDate()); // expected output: 1

// SET FULL YEAR
    const event = new Date('August 19, 1975 23:15:30');

    event.setFullYear(1969);
    console.log(event.getFullYear()); // expected output: 1969

    event.setFullYear(0);
    console.log(event.getFullYear()); // expected output: 0

// SET HOURS
    const event = new Date('August 19, 1975 23:15:30');
    event.setHours(20);
    console.log(event); // expected output: Tue Aug 19 1975 20:15:30 GMT+0200 (CEST)
    // (note: your timezone may vary)

    event.setHours(20, 21, 22);
    console.log(event); // expected output: Tue Aug 19 1975 20:21:22 GMT+0200 (CEST)

// SET MILLISECONDS
    const event = new Date('August 19, 1975 23:15:30');
    console.log(event.getMilliseconds()); // expected output: 0

    event.setMilliseconds(456);
    console.log(event.getMilliseconds()); // expected output: 456

// SET MINUTES
    const event = new Date('August 19, 1975 23:15:30');
    event.setMinutes(45);
    console.log(event.getMinutes()); // expected output: 45
    console.log(event); // expected output: Tue Aug 19 1975 23:45:30 GMT+0200 (CEST)
    // (note: your timezone may vary)

// SET MONTH
    const event = new Date('August 19, 1975 23:15:30');
    event.setMonth(3);
    console.log(event.getMonth()); // expected output: 3
    console.log(event); // Sat Apr 19 1975 23:15:30 GMT+0100 (CET)
    // (note: your timezone may vary)

// SET SECONDS
    const event = new Date('August 19, 1975 23:15:30');
    event.setSeconds(42);
    console.log(event.getSeconds()); // expected output: 42
    console.log(event); // Sat Apr 19 1975 23:15:42 GMT+0100 (CET)
    // (note: your timezone may vary)

// SET TIME
    const event1 = new Date('July 1, 1999');
    const event2 = new Date();
    event2.setTime(event1.getTime());

    console.log(event1); // expected output: Thu Jul 01 1999 00:00:00 GMT+0200 (CEST)
    console.log(event2); // expected output: Thu Jul 01 1999 00:00:00 GMT+0200 (CEST)
    // (note: your timezone may vary)

// _____________
// SET UTC DATES
// _____________

// SET UTC DATE
    const event = new Date('August 19, 1975 23:15:30 GMT-3:00');
    console.log(event.getUTCDate()); // expected output: 20

    event.setUTCDate(19);
    console.log(event.getUTCDate()); // expected output: 19

// SET UTC FULL YEAR
    const event = new Date('December 31, 1975 23:15:30 GMT-3:00');

    console.log(event.toUTCString()); // Thu, 01 Jan 1976 02:15:30 GMT
    console.log(event.getUTCMonth()); // expected output: 0

    event.setUTCMonth(11);
    console.log(event.toUTCString()); // expected output: Wed, 01 Dec 1976 02:15:30 GMT

// SET UTC HOURS
    const event = new Date('August 19, 1975 23:15:30 GMT-3:00');
    console.log(event.toUTCString()); // expected output: Wed, 20 Aug 1975 02:15:30 GMT
    console.log(event.getUTCHours()); // expected output: 2

    event.setUTCHours(23);
    console.log(event.toUTCString()); // expected output: Wed, 20 Aug 1975 23:15:30 GMT

// SET UTC MILLISECONDS
    const date1 = new Date('2018-01-24T12:38:29.069Z');
    console.log(date1.getUTCMilliseconds()); // expected output: 69

    date1.setUTCMilliseconds(420);
    console.log(date1.getUTCMilliseconds()); // expected output: 420

// SET UTC MINUTES
    const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');
    console.log(date1.getUTCMinutes()); // expected output: 15

    date1.setUTCMinutes(25);
    console.log(date1.getUTCMinutes()); // expected output: 25

// SET UTC MONTH
    const event = new Date('December 31, 1975 23:15:30 GMT-3:00');

    console.log(event.toUTCString()); // Thu, 01 Jan 1976 02:15:30 GMT
    console.log(event.getUTCMonth()); // expected output: 0

    event.setUTCMonth(11);
    console.log(event.toUTCString()); // expected output: Wed, 01 Dec 1976 02:15:30 GMT

// SET UTC SECONDS
    const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

    console.log(date1.getUTCSeconds()); // expected output: 30

    date1.setUTCSeconds(39);
    console.log(date1.getUTCSeconds()); // expected output: 39

Enter fullscreen mode Exit fullscreen mode

Date Convertions

//Date.now - method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
console.log(Date.now()); //1613449122179 

// USING PARSE FUNCTION
    const unixTimeZero = Date.parse('01 Jan 1970 00:00:00 GMT');
    const javaScriptRelease = Date.parse('04 Dec 1995 00:12:00 GMT');

    console.log(unixTimeZero); // expected output: 0
    console.log(javaScriptRelease); // expected output: 818035920000

// TO DATE STRING
    const event = new Date(1993, 6, 28, 14, 39, 7);

    console.log(event.toString()); // expected output: Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)
    // (note: your timezone may vary)
    console.log(event.toDateString()); // expected output: Wed Jul 28 1993

// TO ISO STRING
    const event = new Date('05 October 2011 14:48 UTC');
    console.log(event.toString()); // expected output: Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)
    // (note: your timezone may vary)
    console.log(event.toISOString()); // expected output: 2011-10-05T14:48:00.000Z

// TO JSON
    const event = new Date('August 19, 1975 23:15:30 UTC');
    const jsonDate = event.toJSON();

    console.log(jsonDate); // expected output: 1975-08-19T23:15:30.000Z
    console.log(new Date(jsonDate).toUTCString()); // expected output: Tue, 19 Aug 1975 23:15:30 GMT

// TO LOCAL DATE SCTRING
    const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
    const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

    console.log(event.toLocaleDateString('de-DE', options)); // expected output: Donnerstag, 20. Dezember 2012
    console.log(event.toLocaleDateString('ar-EG', options)); // expected output: الخميس، ٢٠ ديسمبر، ٢٠١٢
    console.log(event.toLocaleDateString(undefined, options)); // expected output: Thursday, December 20, 2012 (varies according to default locale)


// TO LOCALE STRING
    const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

    // British English uses day-month-year order and 24-hour time without AM/PM
    console.log(event.toLocaleString('en-GB', { timeZone: 'UTC' })); // expected output: 20/12/2012, 03:00:00

    // Korean uses year-month-day order and 12-hour time with AM/PM
    console.log(event.toLocaleString('ko-KR', { timeZone: 'UTC' })); // expected output: 2012. 12. 20. 오전 3:00:00

// TO LOCAL TIME STRING
    // Depending on timezone, your results will vary
    const event = new Date('August 19, 1975 23:15:30 GMT+00:00');
    console.log(event.toLocaleTimeString('en-US')); // expected output: 1:15:30 AM
    console.log(event.toLocaleTimeString('it-IT')); // expected output: 01:15:30
    console.log(event.toLocaleTimeString('ar-EG')); // expected output: ١٢:١٥:٣٠ ص

// TO STRING
    const event = new Date('August 19, 1975 23:15:30');
    console.log(event.toString()); // expected output: Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)
    // (note: your timezone may vary)

// TO TIME STRING
    const event = new Date('August 19, 1975 23:15:30');
    console.log(event.toTimeString()); // expected output: 23:15:30 GMT+0200 (CEST)
    // (note: your timezone may vary)

// TO UTC STRING
    const event = new Date('14 Jun 2017 00:00:00 PDT');
    console.log(event.toUTCString()); // expected output: Wed, 14 Jun 2017 07:00:00 GMT

// USING VALUE OF FUNCTION
    const date1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));
    console.log(date1.valueOf()); // expected output: 823230245000

    const date2 = new Date('02 Feb 1996 03:04:05 GMT');
    console.log(date2.valueOf()); // expected output: 823230245000

Enter fullscreen mode Exit fullscreen mode


This content originally appeared on DEV Community and was authored by Jenuel Oras Ganawed


Print Share Comment Cite Upload Translate Updates
APA

Jenuel Oras Ganawed | Sciencx (2021-02-16T05:30:12+00:00) JavaScript: Date Cheat Sheet. Retrieved from https://www.scien.cx/2021/02/16/javascript-date-cheat-sheet/

MLA
" » JavaScript: Date Cheat Sheet." Jenuel Oras Ganawed | Sciencx - Tuesday February 16, 2021, https://www.scien.cx/2021/02/16/javascript-date-cheat-sheet/
HARVARD
Jenuel Oras Ganawed | Sciencx Tuesday February 16, 2021 » JavaScript: Date Cheat Sheet., viewed ,<https://www.scien.cx/2021/02/16/javascript-date-cheat-sheet/>
VANCOUVER
Jenuel Oras Ganawed | Sciencx - » JavaScript: Date Cheat Sheet. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/16/javascript-date-cheat-sheet/
CHICAGO
" » JavaScript: Date Cheat Sheet." Jenuel Oras Ganawed | Sciencx - Accessed . https://www.scien.cx/2021/02/16/javascript-date-cheat-sheet/
IEEE
" » JavaScript: Date Cheat Sheet." Jenuel Oras Ganawed | Sciencx [Online]. Available: https://www.scien.cx/2021/02/16/javascript-date-cheat-sheet/. [Accessed: ]
rf:citation
» JavaScript: Date Cheat Sheet | Jenuel Oras Ganawed | Sciencx | https://www.scien.cx/2021/02/16/javascript-date-cheat-sheet/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.