How to convert today date numeric to string today's

I would like to show today instead of not showing Date(23-07-2020) in numeric values. Is it possible?

Like this picture I need to show the date as today or yesterday

enter image description here

var today = new Date();
var date = String(today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate());
var time = today.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
var dateTime = date+' '+time;
this.dateTimeValue = dateTime;

Upvotes: 1

Views: 119

Answers (1)

shankar upadhyay
shankar upadhyay

Reputation: 941

There are two approaches to achieve it:-

  1. By using some library such as momentjs
  2. Or by using the native methods and writing the logic for all calcualtios.

1st approach:-

If you want to go with the first approach you can find the link here

you can use the functions toNow() or fromNow() depending upon the requirement Here as per your question fromNow() is suitable

Here is the link code Pen link

and below is the code snippet:-

let date = moment("2020-07-23", "YYYY-MM-DD");
document.write(date.fromNow());

which is using momentjs library

2nd approach:-

A similar question can be find question link

for which the code pen link is here code pen

Upvotes: 2

Related Questions