Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elegantly check if a given date is yesterday

Assuming you have a Unix timestamp, what would be an easy and/or elegant way to check if that timestamp was some time yesterday?

I am mostly looking for solutions in Javascript, PHP or C#, but pseudo code and language agnostic solutions (if any) are welcome as well.

like image 475
Aistina Avatar asked Jul 09 '10 10:07

Aistina


People also ask

How do I find yesterday's date?

How do you get yesterdays' date using JavaScript? We use the setDate() method on yesterday , passing as parameter the current day minus one. Even if it's day 1 of the month, JavaScript is logical enough and it will point to the last day of the previous month.

How do you get yesterday date IN react native?

Getting Yesterday's date First, we need to access the today's date using new Date() constructor then we need to subtract it with 1 . In the above code, we used the setDate() method on today and passed today's date minus 1 as arguments to get yesterday's date.

How do I get the past date in flutter?

How to select previous or next dates based on the selected date in the Flutter Date Range Picker (SfDateRangePicker) In the Flutter Date Range Picker, you can highlight the before and after date ranges of the selected date by using onSelectionChanged callback of the Flutter date range picker.


2 Answers

In C# you could use this:

bool isYesterday = DateTime.Today - time.Date == TimeSpan.FromDays(1);

like image 111
Omer Mor Avatar answered Oct 01 '22 23:10

Omer Mor


You can use this in C#:

bool isYesterday = (dateToCheck.Date.AddDays(1) == DateTime.Now.Date);
like image 42
onof Avatar answered Oct 01 '22 23:10

onof