(Javascript) My learning journey: Numbers, Dates and Timer
Tags: javascript, beginners, tutorial
Published: 2021-03-04 01:59:59

---
title: (Javascript) My learning journey: Numbers, Dates and Timer
description:
published: true
tags: javascript, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik1c8mr2sz8zi3g466dq.jpg
series: (Javascript) My learning journey
---

An essential point to remember a training course is to take notes and discuss the subject with others. That's why every day I will post on dev.to the concepts and notes that I learned the day before.

If you want to miss nothing click follow and you are welcome to comments and discuss with me.

Without further ado here is a summary of my notes for today.

<h2>Numbers</h2>
In Javascript numbers are always represented as decimal
```js
console.log(20 === 20.0) // true
```
In Javascript numbers are represent internally by binary base
```js
console.log(0.1 + 0.2 === 0.3) // false because of binary rounding
```

Numbers methods
```js
// convert string to number
console.log(Number('20')) // 20
// or
console.log(+'20') // 20

// parsing (extracting) number from string
console.log(Number.parseInt('100px') // 100
console.log(Number.parseFloat('100.5 pixel') // 100.5

// Check if is number
console.log(Number.isFinite(20)) // true
console.log(Number.isFinite('20')) // false
console.log(Number.isFinite('Test 20')) // false
```

Math 
```js
// Max
console.log(Math.max(5, 10, 25, 7, 2) // 25
console.log(Math.max(5, 10, '25', 7, 2) // 25
  
// Random between 0 and 1
console.log(Math.random()) // 0.92

// Rounding integer
console.log(Math.trunc(20.5)) // 20

// Round
console.log(Math.round(20.5)) // 21

// Ceil
console.log(Math.ceil(20.2)) // 21

// Floor
console.log(Math.floor(20.5)) // 20

// Round decimal (string decimal)
console.log((20.5).toFixed(2)) // '21.50'
console.log(+(20.5).toFixed(2)) // 21.50

```

Remainder operator 
```js
console.log(10 % 2) // 0
console.log(5 % 2) // 1

// Check if number is even
console.log(6 % 2 === 0) // true
console.log(5 % 2 === 0) // false

// Check if number is odd
console.log(6 % 2 !== 0) // false
console.log(5 % 2 !== 0) // true
```
<h2>Date and Time</h2>
```js
// Create a date
const now = new Date()
const myDate = new Date('Aug 02 1999')
const myLongDate = new Date('January 1, 2021')
// Month is zero base (0 = January, 1 = February, ...)
const dateTime = new Date(2021, 02, 28, 22, 17, 30)
console.log(now) // Wed Mar 03 2021 12:47:59 GMT-0500
console.log(myDate) // Mon Aug 02 1999 00:00:00 GMT-0400
console.log(myLongDate) // Fri Jan 01 2021 00:00:00 GMT-0500
console.log(dateTime) // Sun Mar 28 2021 22:17:30 GMT-0400

```
Date methods
```js
const firstDate = new Date('January 1, 2021')
console.log(firstDate.getFullYear()) // 2021
console.log(firstDate.getMonth()) // 1
console.log(firstDate.getDate()) // 5 (zero base)
console.log(firstDate.getDay()) // 1
console.log(firstDate.getHours())
console.log(firstDate.getMinutes())
console.log(firstDate.getSeconds())

// Set
firstDate.setFullYear = 2021

// Current timestamps
console.log(Date.now())

```

Date internationalisation
```js
const firstDate = new Date('January 1, 2021')
console.log(new Intl.DateTimeFormat('en-US').format(firstDate)) // 1/1/2021
console.log(new Intl.DateTimeFormat('fr-CA').format(firstDate)) // 2021-01-01
```

Format date options
```js
const options = {
    hour: 'numeric',
    minute: 'numeric',
    day: 'numeric',
    month: 'long',
    year: 'numeric',
    weekday: 'short',
  }
  const firstDate = new Date('January 1, 2021')
  console.log(new Intl.DateTimeFormat('en-US', options).format(firstDate)) // Fri, January 1, 2021, 12:00 AM
```

Get local from navigator
```js
console.log(navigator.language) // en-US
```

Numbers internationalisation
```js
console.log(new Intl.NumberFormat('en-US').format(9999.95)) // 9,999.95
console.log(new Intl.NumberFormat('fr-CA').format(9999.95)) // 9 999,95

const options = {
    style: 'currency',
    currency: 'USD' // CAD for Canadian
}
console.log(new Intl.NumberFormat('en-US', options).format(9999.95)) // $9,999.95
```

<h2>Timer</h2>
```js
// Wait 5 seconds
setTimeout(() => console.log('5 sec wait'), 5000)
console.log('Waiting...')

// pass parameters to setTimeout
setTimeout((message) => console.log(`${message}`), 5000, 'Wait is over')

// Clear Timer
const timer1 = setTimeout((message) => console.log(`${message}`), 5000, 'Wait is over')
const condition = true
if (condition) clearTimeout(timer1)

// Set Interval
setInterval(() => console.log('Execute every 3 sec'), 3000)
```