Skip to content

Commit 36870ec

Browse files
committed
09 Date and Time in JS
Learned about Date and Time in Javascript and some methods related to Dates (Converting it into more readable format), declaring specific date, time stamps, converting from millisec to sec e.t.c.
1 parent d652a61 commit 36870ec

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

01_Basics/08_nums_and_maths.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const newBalance = new Number(400)
1616
// Some Methods,
1717
// console.log(newBalance.toString()); // Output: 400
1818
// Note: When we will check it's typeof the we will see that it is changed into strings.
19-
// console.log(newBalance.toString().length); // Output: 3 (cause 100 has 3 chracters.)
19+
// console.log(newBalance.toString().length); // Output: 3 (cause 400 has 3 chracters.)
2020

2121
// console.log(newBalance.toFixed(2)); // Output: 400.00
2222

@@ -33,7 +33,7 @@ const hundreds = 1000000
3333
3434
(ii) Maths, */
3535
console.log(Math); /* Output: Object [Math] {}
36-
Note: When we run this console.log(Math) in browser's console we will get value under object. */
36+
Note: When we run this console.log(Math) in browser's console we will get many value inside object. */
3737

3838
console.log(Math.abs(-4)); // Output: 4 (Changes only minus values in plus)
3939

@@ -47,7 +47,7 @@ Note: There is also "max". */
4747

4848
console.log(Math.random()); // Output: 0.36675387800250636 (Gives random numbers between 0 & 1)
4949
console.log((Math.random()*10) + 1); // Now numbers will range from 1 to 9
50-
console.log(Math.floor(Math.random()*10) + 1); // Round offs at Random numbers at lowest value.
50+
console.log(Math.floor(Math.random()*10) + 1); // Round offs the Random numbers at lowest value.
5151

5252
const min = 10
5353
const max = 20

01_Basics/09_datesIn.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* 9. Dates
2+
3+
Note: Our declared Dates has been calculated from 1 January 1970.
4+
Note: Dates has been calculated usually in Milliseconds. */
5+
6+
let myDate = new Date();
7+
console.log(myDate); /* Output: 2024-04-24T08:15:11.671Z
8+
9+
--------------------------------------------------------------------------------------
10+
11+
Conversion of Dates in readable form,
12+
Some Methods, */
13+
console.log(myDate.toString()); // Output: Wed Apr 24 2024 13:47:36 GMT+0530 (India Standard Time)
14+
console.log(myDate.toDateString()); // Output: Wed Apr 24 2024
15+
console.log(myDate.toLocaleString()); // Output: 24/4/2024, 1:49:14 pm
16+
console.log(myDate.toJSON()); // Output: 2024-04-24T08:20:04.530Z
17+
console.log(myDate.toISOString()); // Output: 2024-04-24T08:21:49.915Z
18+
console.log(myDate.toLocaleDateString()); /* Output: 24/4/2024
19+
20+
--------------------------------------------------------------------------------------
21+
22+
Typeof Date, */
23+
console.log(typeof myDate); /* Output: Object
24+
25+
--------------------------------------------------------------------------------------
26+
27+
Declaring a new or a specific date, */
28+
// let myCreatedDate = new Date(2002, 10, 24) // Months starts with 0 in JS.
29+
// console.log(myCreatedDate.toDateString()); // Output: Sun Nov 24 2002
30+
// let myCreatedDate = new Date(2002, 10, 24, 5, 3)
31+
// console.log(myCreatedDate.toLocaleString()); // Output: 24/11/2002, 5:03:00 am
32+
let myCreatedDate = new Date("11-24-2002")
33+
console.log(myCreatedDate.toLocaleString()); /* Output: 24/11/2002, 5:30:00 am
34+
35+
-------------------------------------------------------------------------------------- */
36+
37+
let myTimeStamp = Date.now()
38+
console.log(myTimeStamp); // Output: 1713948664801 (Millisecond value from 1 Jan 1970 to current)
39+
console.log(myCreatedDate.getTime()); /* Output: 1038076200000
40+
So now we can easily compare 1713948664801 and 1038076200000.
41+
42+
Converting it into seconds, */
43+
console.log(Math.floor(Date.now()/1000)); // Output: 1713950199
44+
45+
let myDates = new Date()
46+
console.log(myDates);
47+
console.log(myDates.getDay()); // Output: 3
48+
console.log(myDates.getMonth() + 1); /* Output: 4
49+
50+
----------------------------------------------------------------------------------------
51+
52+
Some other syntax, */
53+
`${myDates.getDay()} and the time is`
54+
// We can completely define what we want,
55+
myDates.toLocaleString('default', {
56+
weekday: "long"
57+
})
58+
59+
60+
61+

0 commit comments

Comments
 (0)