views:

301

answers:

10

Hi,

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.

A: 

Check if it is between yesterday midnight and last midnight.

Sjoerd
+2  A: 

In C# you could use this:

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

Omer Mor
That only works if the timestamp was at midnight exactly. Edit: Works with <= though :) Edit 2: Disregard that, this does work :P
Aistina
Omer Mor
A: 

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;
 }
rakuo15
+2  A: 

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;
}
Artefacto
+1  A: 

Another C# example:

bool isYesterday = DateTime.Now.Date.AddDays(-1) == dateToCheck.Date;
w69rdy
Why so negative? :-)I'd write that as:`bool isYesterday = dateToCheck.Date.AddDays(1) == DateTime.Today;`
Omer Mor
@Omer Mor lol thats just my pessimism showing through ;)
w69rdy
+1  A: 

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");
    }
}
jgauffin
A: 

C#:

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

onof
+5  A: 

PHP:

$isYesterday = date('Ymd', $timestamp) == date('Ymd', strtotime('yesterday'));
Alexander Konstantinov
+4  A: 

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.

Kwebble
A: 

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
    });
})();
Anurag