Languages
[Edit]
EN

TypeScript - get current time

0 points
Created by:
evangeline
420

In TypeScript Date class is useful to get current time.

Quick solution:

const now: number = Date.now();

console.log('Current unix millisecond time: ' + now);

 

1. Predefined methods example

const now: Date = new Date();

console.log('Current unix millisecond time: ' + now.getTime());
console.log('Current date: ' + now.toDateString());
console.log('Current time: ' + now.toTimeString());
console.log('Current date & time: ' + now.toString());

Note: during using to...String methods it is necessaryto be careful because of specific formatting of data and time on the locale.

3. Custom formatting

3.1. Example 1

renderNumber method can be replaced with built-in padStart method.

// ONLINE-RUNNER:browser;

const renderNumber = (value: number, length: number) => {
  let result: string = String(value);

  for (; length > result.length; length -= 1) result = '0' + result;

  return result;
};

const renderDate = (date: Date) => {
  const year: string = renderNumber(date.getFullYear(), 4);
  const month: string = renderNumber(date.getMonth() + 1, 2);
  const day: string = renderNumber(date.getDate(), 2);

  return `${year}.${month}.${day}`;
};

const renderTime = (date: Date) => {
  const hours: string = renderNumber(date.getHours(), 2);
  const minutes: string = renderNumber(date.getMinutes(), 2);
  const seconds: string = renderNumber(date.getSeconds(), 2);
  const milliseconds: string = renderNumber(date.getMilliseconds(), 3);

  return `${hours}:${minutes}:${seconds}.${milliseconds}`;
};

const renderDateTime = (date: Date) => {
  const dateText: string = renderDate(date);
  const timeText: string = renderTime(date);

  return `${dateText} ${timeText}`;
};

// Usage example:

const now: Date = new Date();

const date: string = renderDate(now);
const time: string = renderTime(now);
const dateTime: string = renderDateTime(now);

console.log('Current date: ' + date);
console.log('Current time: ' + time);
console.log('Current date & time: ' + dateTime);

3.1. Example 2

// ONLINE-RUNNER:browser;

function renderNumber(value: number, length: number) {
  let result: string = String(value);
  for (; length > result.length; length -= 1) {
    result = '0' + result;
  }
  return result;
}

function renderDate(date: Date) {
  const year: number = date.getFullYear();
  const month: number = date.getMonth() + 1;
  const day: number = date.getDate();

  const text: string =
    renderNumber(year, 4) +
    '.' +
    renderNumber(month, 2) +
    '.' +
    renderNumber(day, 2);
  return text;
}

function renderTime(date: Date) {
  const hour: number = date.getHours();
  const minute: number = date.getMinutes();
  const second: number = date.getSeconds();
  const millisecond: number = date.getMilliseconds();

  const text: string =
    renderNumber(hour, 2) +
    ':' +
    renderNumber(minute, 2) +
    ':' +
    renderNumber(second, 2) +
    '.' +
    renderNumber(millisecond, 3);
  return text;
}

function renderDateTime(date: Date) {
  const dateText: string = renderDate(date);
  const timeText: string = renderTime(date);

  return dateText + ' ' + timeText;
}

// Usage example:

const now: Date = new Date();

const date: string = renderDate(now);
const time: string = renderTime(now);
const dateTime: string = renderDateTime(now);

console.log('Current date: ' + date);
console.log('Current time: ' + time);
console.log('Current date & time: ' + dateTime);
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join