Day 7 : Converting Variable to Numbers  And Date Set Methods ?

Jemin KikaniJemin Kikani
4 min read

Converting Variable to Numbers

There are 3 JavaScript methods that can be used to convert a variable to a number:

Method 

Description

Number() 

Returns a number converted from argument.

parseFloat() 

Parse its argument and returns a floating point number

parseInt() 

Parse its argument and returns a whole number

Example:-

Number(true); // 1

Number(false); // 0

Number("10"); // 10

Number(" 10"); // 10

Number("10 "); // 10

Number(" 10 "); // 10

Number("10.33"); // 10.33

Number("10,33"); // NaN

Number("10 33"); // NaN

Number("John"); // NaN

parseInt("-10"); // -10

parseInt("-10.33"); // -10

parseInt("10"); // 10

parseInt("10.33"); // 10

parseInt("10 20 30"); // 10

parseInt("10 years"); // 10

parseInt("years 10"); // NaN

parseFloat("10"); // 10

parseFloat("10.33"); // 10.33

parseFloat("10 20 30"); // 10

parseFloat("10 years"); // 10

parseFloat("years 10"); // NaN

Number Object Method

These object methods belong to the Number object:

Method 

Description

Number.isInteger() 

Returns true if the argument is an integer

Number.isSafeInteger() 

Returns true if the argument is a safe integer

Number.parseFlaot() 

Convert a string to a number

Number.parseInt() 

Convert a string to a whole number

Example:-

Number.isInteger(10); // true

Number.isInteger(10.5); // false

Number.isSafeInteger(10); // true

Number.isSafeInteger(12345678901234567890); // false

Number.parseFloat("10"); // 10

Number.parseFloat("10.33"); // 10.33

Number.parseFloat("10 20 30"); // 10

Number.parseFloat("10 years"); // 10

Number.parseFloat("years 10"); // NaN

Number.parseInt("-10"); // -10

Number.parseInt("-10.33"); // -10

Number.parseInt("10"); // 10

Number.parseInt("10.33"); // 10

Number.parseInt("10 6"); // 10

Number.parseInt("10 years"); // 10

Number.parseInt("years 10"); // NaN

Date Object

Date objects are static. The "clock" is not "running".

The computer clock is ticking, date objects are not.

By default, JavaScript will use the browser's time zone and display a date as a full text string:

sun march 10 2024 17:10:57 GMT+0530 (India Standard Time)

Creating Date Object

Date objects are created with the new Date() constructor.

There are 9 ways to create a new date object

new Date()

new Date(date string)

new Date(year,month)

new Date(year,month,day)

new Date(year,month,day,hours)

new Date(year,month,day,hours,minutes)

new Date(year,month,day,hours,minutes,seconds)

new Date(year,month,day,hours,minutes,seconds,ms)

new Date(milliseconds)

Notes:-

JavaScript counts months from 0 to 11. January = 0 and December = 11 Specifying a month higher than 11, will not result in an error but add the overflow to the next year.

Specifying a day higher than max, will not result in an error but add the overflow to the next month.

One and two digit years will be interpreted as 19xx.

Example:-

const d = new Date();

// Wed Dec 28 2022 17:50:34 GMT+0530 (India Standard Time)

const d = new Date("October 13, 2014 11:13:00"); // (Date String) // Mon Oct 13 2014 11:13:00 GMT+0530 (India Standard Time)

const d = new Date(2018, 11, 24, 10, 33, 30, 0); // (year,month,….) // Mon Dec 24 2018 10:33:30 GMT+0530 (India Standard Time)

const d = new Date(99, 11, 24); // Previous Century

// Fri Dec 24 1999 00:00:00 GMT+0530 (India Standard Time)

const d = new Date(100000000000); // (Milliseconds)

// Sat Mar 03 1973 15:16:40 GMT+0530 (India Standard Time)

Date Methods

When a date object is created, a number of methods allow you to operate on it.

Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.

When you display a date object in HTML, it is automatically converted to a string, with the toString() method.

Example:-

const d = new Date();

d.toString();

// Wed Dec 28 2022 17:50:34 GMT+0530 (India Standard Time) The toDateString() method converts a date to a more readable format. Example:-

const d = new Date();

d.toDateString();

// Wed Dec 28 2022

The toUTCString() method converts a date to a string using the UTC standard. Example:-

const d = new Date();

d.toUTCString();

// Wed Dec 28 2022 17:50:34 GMT

The toISOString() method converts a date to a string using the ISOstandard. ISO dates can be written with added hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SSZ)

Example:-

const d = new Date();

d.toISOString();

// 2022-12-28T05:05:50.697Z

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 Minutes (0-59)

getSeconds() 

Get Seconds (0-59)

getMilliSeconds() 

Get MilliSeconds (0-999)

getTime() 

Get Time (milliseconds since january 1, 1970)

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 returns the difference (in minutes) between local time an UTC time.

Example:-

let diff = d.getTimezoneOffset(); // -330

Date Set Methods

Method 

Description

setFullYear() 

Set the Year (optionally month and day) Set the Month as a number (0-11)

setDate() 

Set the Day as a number (1-31)

setDay() 

Set the WeekDay as a number (0-6)

setHours() 

Set the Hours (0-23)

setMinutes() 

Set the Minutes (0-59)

setSeconds() 

Set the Seconds (0-59)

setMilliSeconds() 

Set the MilliSeconds (0-999)

setTime() 

Set the Time (milliseconds since January 1, 1970)

0
Subscribe to my newsletter

Read articles from Jemin Kikani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jemin Kikani
Jemin Kikani