15

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.

11 Answers 11

24

In C# you could use this:

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

3
  • 1
    That only works if the timestamp was at midnight exactly. Edit: Works with <= though :) Edit 2: Disregard that, this does work :P
    – Aistina
    Jul 9, 2010 at 11:09
  • 4
    Nope: Both Today & Date properties give back only the date part of the date-time, with the time part being zero. So it should work fine.
    – Omer Mor
    Jul 9, 2010 at 11:14
  • 1
    I think I'd rather rephrase the answer to the following: var yesterday = DateTime.Today - TimeSpan.FromDays(1); bool isYesterday = time.Date == yesterday;
    – Omer Mor
    Apr 17, 2018 at 19:16
13

You can use this in C#:

bool isYesterday = (dateToCheck.Date.AddDays(1) == DateTime.Now.Date);
0
10

PHP:

$isYesterday = date('Ymd', $timestamp) == date('Ymd', strtotime('yesterday'));
5

In pseudo code, to compare timestamps:

  1. get current Unix timestamp
  2. transform the retrieved timestamp to a date
  3. subtract 1 day from the date
  4. transform the timestamp to test to a date
  5. compare both dates. If they're equal the tested timestamp was yesterday.

Watch out for timezones if you show the results to a user. For me it's now 13:39 on July 9 2010. A timestamp for 14 hours ago for me is yesterday. But for someone in a different timezone where it's now 15:39, 14 hours ago wasn't yesterday!

Another problem might be systems with a wrong time/date setup. For example if you use JavaScript and the system time of the visitors PC is wrong, the program may come to a wrong conclusion. If it's essential to get a correct answer, retrieve the current time from a known source with a correct time.

3

An example in Smalltalk using Pharo/Squeak

(Date year: 2014 month: 4 day: 24) = Date yesterday
2

This accepts an optional DateTimeZone object. If it's not given, it uses the currently set default timezone.

<?php
function isYesterday($timestamp, $timezone = null) {
    $t = new DateTime(null, $timezone);
    $t->setTimestamp($timestamp);
    $t->setTime(0,0);
    $yesterday = new DateTime("now", $timezone);
    $yesterday->setTime(0,0);
    $yesterday = $yesterday->sub(new DateInterval('P1D'));

    return $t == $yesterday;
}
2

Another C# example:

bool isYesterday = DateTime.Now.Date.AddDays(-1) == dateToCheck.Date;
1
  • 5
    Why so negative? :-) I'd write that as: bool isYesterday = dateToCheck.Date.AddDays(1) == DateTime.Today;
    – Omer Mor
    Jul 9, 2010 at 11:16
2

Code:

static class ExtensionMethods
{
    private static readonly DateTime UnixStart = new DateTime(1970, 1, 1, 0, 0, 0, 0);;

    public static bool IsYesterday(this int unixTime)
    {
        DateTime convertedTime = UnixStart.AddSeconds(unixTime);
        return convertedTime.Date == DateTime.Now.AddDays(-1).Date;
    }

    public static bool IsYesterday(this DateTime date)
    {
        return date.Date == DateTime.Now.AddDays(-1).Date;
    }
}

Examples:

public class Examples
{
    public void Tests()
    {
        if (1278677571.IsYesterday()) System.Console.WriteLine("Is yesterday");

        DateTime aDate = new DateTime(2010, 12, 31);
        if (aDate.IsYesterday()) System.Console.WriteLine("Is yesterday");
    }
}
1

In JavaScript, you could write

var someDate = new Date(2010, 6, 9);
Date.yesterday.date == someDate.date // true

Left out needless implementation details, but it's possible. Ok, there ya go :)

(function() {
    function date(d) {
        var year = d.getFullYear();
        var month = d.getMonth();
        var day = d.getDate();
        return new Date(year, month, day);
    }

    Object.defineProperty(Date, 'yesterday', {
        enumerable: true,
        configurable: false,
        get: function() {
            var today = new Date();
            var millisecondsInADay = 86400000;
            var yesterday = new Date(today - millisecondsInADay);
            return yesterday;
        },
        set: undefined
    });​​​​​​​​

    Object.defineProperty(Date.prototype, 'date', {
        enumerable: true,
        configurable: true,
        get: function() {
            return date(this).valueOf();
        },
        set: undefined
    });
})();
1

C#

    TimeSpan difference = DateTime.Now.Date - olderDate.Date;
    bool isYesterday = difference.TotalDays == 1;
-1

You can give this function a shot:

public bool IsFromYesterday(long unixTime) {
    DateTime convertedTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    convertedTime.AddSeconds(unixTime);

    DateTime rightNow = DateTime.Now;

    DateTime startOfToday = DateTime.Today;
    DateTime startOfYesterday = startOfToday - new TimeSpan(1, 0, 0, 0);

    if (convertedTime > startOfYesterday && convertedTime < rightNow)
        return true;
    else
        return false;
 }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.