Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current date and time in TypeScript

Tags:

typescript

Summary

I'm creating my first extension for VSCode in TypeScript, and want to create a new information message to display current date/time.

What I've tried

I've tried looking for some data/time keyword in the list of vscode key bindings. I've also tried looking for a function to get system date/time on Google only to come across solutions explaining data/time syntax and how to display a specific date/time but nothing to get the CURRENT data/time. I've also looked for it in vscode API documentation.

Code part

According to my understanding the code should be in extension.ts file in activate section where I register the command to implement showInformationMessage function call and send a string to it containing the date/time. So here is the code around the line where I store the current date/time:

   let disposableShowTime = vscode.commands.registerCommand(     "extension.showTime",     () => {       // Display a message box to the user       let dateTime = "22 March, 2019 12:45PM"; // store current data/time instead       vscode.window.showInformationMessage("Current time: " + dateTime);     }   ); 

Desired output: [Current Date/Time]

Actual output: 22 March, 2019 12:45PM

like image 248
fidgetyphi Avatar asked Mar 22 '19 19:03

fidgetyphi


People also ask

Is there dateTime type in TypeScript?

The Date object represents a date and time functionality in TypeScript. It allows us to get or set the year, month and day, hour, minute, second, and millisecond.

How do you write a Date in TypeScript?

Every class or interface can be used as a type in TypeScript. const date = new Date();


2 Answers

If you use angular and Moment type

import * as moment from 'moment'; ... //Change DATE_TIME_FORMAT by the format need const DATE_TIME_FORMAT = 'YYYY-MM-DDTHH:mm';  let _now: Moment; _now = moment(new Date(), DATE_TIME_FORMAT); 
like image 22
Salim Hamidi Avatar answered Sep 18 '22 07:09

Salim Hamidi


To retrieve the current system Date/Time in javaScript/TypeScript you need to create a new Date object using parameterless constructor, like this:

let dateTime = new Date() 
like image 128
Jurica Smircic Avatar answered Sep 17 '22 07:09

Jurica Smircic