Javascript : Display "Today" from today's date or "Yesterday" from yesterday's date

javascript display text
style display visible
display javascript object in html
how to check div is display:none or block in javascript
toggle display javascript
onclick javascript
javascript display: none by class
javascript display block not working

I am working on Date functionality. I stuck at one point. I get date in YYYY-MM-DD format. So let say today is 2019-02-27 then I would like to get "Today" instead of date. Then yesterday was 2019-02-26 so I would like to get "Yesterday" and other date like 2019-02-25 would be displayed just as it is.

Note : I get date value as a string.

Any help would be great.

Thank You..

You can use moment.js library

let date= new Date();
let dateyday=new Date("2019-02-26");//pass date as a string

let c=moment(date).calendar(); 
console.log(c)
console.log(moment(dateyday).calendar())
console.log(c.split(" ")[0])//split to get today
console.log(moment(dateyday).calendar().split(" ")[0])//split to get yesterday
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>

JavaScript Display Objects, How to Display JavaScript Objects? Displaying a JavaScript object will output [ object Object]. Example. var person = {name:"John� Some common solutions to display JavaScript objects are: Displaying the Object Properties by name Displaying the Object Properties in a Loop Displaying the Object using Object.values ()

Probably a bit too precise for what you need, but using milliseconds etc.

let date1 = new Date('2019-02-27');
let date2 = new Date('2019-02-26');
let date3 = new Date('2018-02-23');
let dateDynamic = new Date(Date.now() - 1000*60*60*12); // date with 12 hours subtrated


const response = (date) => { 

  const day = 1000*60*60*24;
  console.log(day);
  let dteToday = Date.now()-day;
  let dteYesterday = dteToday-day;
  let dteCompare = date.getTime();
  console.log(`${dteToday} - ${dteYesterday} : ${dteCompare}`);
  return (dteToday < dteCompare) ? 'today' : (dteYesterday < dteCompare) ? 'yesterday' : date.toString();
  
};



console.log(response(date1));
console.log(response(date2));
console.log(response(date3));
console.log(response(dateDynamic));

Using CSS together with JavaScript to show content, <p>This panel contains a div element, which is hidden by default (display: none). </p> <p>It is styled with CSS and we use JavaScript to show it (display: block). JavaScript can "display" data in different ways: Writing into an HTML element, using innerHTML. Writing into the HTML output using document.write (). Writing into an alert box, using window.alert ().

This will give you idea to start.

const isToday = (someDate) => {
            const today = new Date()
           return someDate.getDate() == today.getUTCDate() &&
            someDate.getMonth() == today.getUTCMonth() &&
            someDate.getFullYear() == today.getUTCFullYear()
        }; 
        isToday(new Date("2019-02-27")) // true

W3.JS Display, Displaying a Larger Object. To demonstrate the power of W3.JS, we will display a larger JavaScript object (myObject). The object is an array of customers� Here's a quick JavaScript trick to control display settings. All we do is set the display of an element to none, and then use a JavaScript function to toggle the display when a user clicks something else.

JavaScript Output, JavaScript Display Possibilities. JavaScript can "display" data in different ways: Writing into an HTML element, using innerHTML . Writing into the HTML output� In JavaScript, text is displayed in elements. This tutorial focuses on the use of the paragraph or ‘ <p> ‘ element. There are multiple ways to display text on a web page in JavaScript, the easiest is: document.write (" Text to display.

CSS display property, Toggle between hiding and showing an element with JavaScript. Tip: For more information about Display and Visibility, read our CSS Display Tutorial. The user clicks and fires the Javascript, which works fine: Javascript. document.getElementById("Asset").style.display = 'block'; However, the style of the row isn't in line with rest even though it's class attributes are set to 'rrtr' like the rest. If I turn off 'display:none;' on the row and run it showing, the format is fine. Any ideas?

How To Toggle Between Hiding And Showing an Element, The JavaScript style display property is meant for setting and returning the display type of a specified element. Most HTML elements have the� Display Popup Message Box JavaScript provides different built-in functions to display popup messages for different purposes e.g. to display a simple message or display a message and take user's confirmation on it or display a popup to take a user's input value.

Comments
  • I'm not sure what you mean. If you want the strings "Today" and "Yesterday" then just compare the date to today's date and yesterday's date.
  • This fails for the same reason the others do: YYYY-MM-DD format dates are parsed as UTC, so for me it's 28 Feb but the above tells me 27 Feb is "today". Also, not all days are 8.64e7 ms long where daylight saving is observed.
  • @RobG I really hope you did not down vote me based on that. Adding an offset for time zone is not really difficult.
  • Also Date.now() is utc as well. So why would this not work?
  • I downvoted because it returns incorrect results as explained in my comment. If you want to know why it's wrong, ask a question. There isn't enough room in comments to provide a decent answer. There is no need to convert the string to a date, you just need to compare the current day, month and year with their respective timestamp components.
  • "2019-02-27" will be parsed as UTC, but you're comparing it to a local date so it will be wrong for the period of the host timezone offset around midnight (which can be up to 14 hours).
  • @RobG yeah you are right. Anwers is edited accordingly.