ReasonJun

javascript : built-in object (time, json) 본문

Frontend/Javasciprt

javascript : built-in object (time, json)

ReasonJun 2023. 6. 7. 01:13
728x90

time 

const data = new Date();
console.log(date);

const d1 = new Date(2022, 11, 16, 12, 57, 30);
// Dec 16, 2022 12:15:30
//? .getFullYear(), .setFullYear()

console.log(d2.getFullYear()); // Extract year only

data.setFullYear(2023); // specify the year

//? .getMonth() .setMonth()
// Returns/specifies the month. (zero-based numbering)

//? .getDate() .setDate()
// 'Returns / specifies 'day'.

//? .getHours() .setHours()
// Returns/specifies the hour.

//? .getMinutes() .setMinutes()
// Returns/specifies minutes.

//? .getSeconds() / .setSeconds()
// 'Returns / specifies 'seconds'.

//? .getDay()
// Returns the 'day' of the date instance.
// 0 : Sunday 1 : Monday ~ 6 : Saturday
const data1 = new Date();

const day = date.getDay();

console.log(day);

//? .getTime() .setTime()
// Convert/specify to 'ms' of the date instance.

Date.prototype.isAfter = function (data) {
  const a = this.getTime();
  const b = date.getTime();
  return a > b;
};

const d2 = new Date('');
const d3 = new Date('');

console.log(d1.isAfter(d2)); // false
console.log(d2.isAfter(d1)); // true

// Date.now()
// Elapsed since '1970-01-01 00:00:00' (Unix time),
// 'ms' when the method is called is returned.
const time = new Date().getTime();
console.log(Date.now());
console.log(time);


console.log(now.toString()); // Tue Jan 24 2023 15:31:52 GMT+0900 (Korean Standard Time)
console.log(now.toDateString()); // Tue Jan 24 2023
console.log(now.toTimeString()); // 15:31:52 GMT+0900 (Korean Standard Time)
console.log(now.toISOString()); // ISO 8601 => 2023-01-24T06:31:52.984Z
console.log(now.toLocaleString('en-US')); // 1/24/2023, 3:31:52 PM
console.log(now.toLocaleString('ko-KR')); // 2023. 1. 24. 오후 3:31:52

json

// JSON (Javascript Object Notation)

// Standard format for passing data
// Use only letters, numbers, booleans, nulls, objects, and arrays
// A single json can hold only one type of data.
// Use only double quotes for characters
// No trailing comma
// Use the .json extension

// JSON.stringfy() = Convert the data to JSON characters.
// JSON.parse()  = Parses JSON characters and converts them into data.

console.log(JSON.stringify('Hello world!')); // "Hello world!"

console.log(JSON.parse('"Hello world!"')); // Hello world!
728x90
Comments