find the current date and time in javascript

To get the current date and time in JavaScript, you can use the Date object. Here's an example:

index.tsx
const now = new Date();
console.log(now);
42 chars
3 lines

This will output the current date and time in the console, in the format:

index.tsx
Tue Jul 06 2021 14:03:42 GMT-0400 (Eastern Daylight Time)
58 chars
2 lines

You can also format the date and time using various methods of the Date object. Here's an example of formatting the date in a specific way:

index.tsx
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth() + 1;
const day = now.getDate();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();

const formattedDate = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
console.log(`The current date is ${formattedDate} and the current time is ${formattedTime}.`);
556 chars
12 lines

This will output something like:

index.tsx
The current date is 2021-07-06 and the current time is 14:03:42.
65 chars
2 lines

gistlibby LogSnag