New date gettime
You're looking for a way to get the current date and time in JavaScript!
You can use the Date
object to achieve this. Here are a few ways to do it:
Method 1: Using Date.now()
const currentTime = new Date().getTime();
console.log(currentTime); // Output: a timestamp in milliseconds
Method 2: Using Date()
const currentDate = new Date();
console.log(currentDate); // Output: a Date object with the current date and time
Method 3: Using Date.now()
and formatting
const currentTime = new Date().getTime();
const formattedTime = new Date(currentTime).toLocaleTimeString();
console.log(formattedTime); // Output: a formatted string like "HH:MM:SS"
Method 4: Using moment.js
library
const moment = require('moment');
const currentTime = moment().format('YYYY-MM-DD HH:mm:ss');
console.log(currentTime); // Output: a formatted string like "2023-03-15 14:30:00"
Choose the method that best fits your needs!