代码之家  ›  专栏  ›  技术社区  ›  Aistina

优雅地检查给定的日期是否是昨天

  •  11
  • Aistina  · 技术社区  · 14 年前

    假设您有一个Unix时间戳,那么用什么简单和/或优雅的方法来检查该时间戳是否是昨天的某个时间?

    我主要在寻找Javascript、PHP或C#的解决方案,但也欢迎使用伪代码和与语言无关的解决方案(如果有的话)。

    11 回复  |  直到 14 年前
        1
  •  10
  •   Alexander Konstantinov    14 年前

    菲律宾比索:

    $isYesterday = date('Ymd', $timestamp) == date('Ymd', strtotime('yesterday'));
    
        2
  •  23
  •   Omer Mor    14 年前

    在C中,您可以使用:

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

        3
  •  9
  •   onof    6 年前

    你可以用C#:

    bool isYesterday = (dateToCheck.Date.AddDays(1) == DateTime.Now.Date);
    
        4
  •  5
  •   Kwebble    14 年前

    在伪代码中,要比较时间戳:

    1. 获取当前Unix时间戳
    2. 将检索到的时间戳转换为日期
    3. 将要测试的时间戳转换为日期

    如果向用户显示结果,请注意时区。对我来说,现在是2010年7月9日13:39。对我来说,14小时前的时间戳是昨天。但是对于一个不同时区的人来说,现在是15:39,14小时前不是昨天!

    另一个问题可能是系统的时间/日期设置错误。例如,如果您使用JavaScript并且visitors PC的系统时间是错误的,那么程序可能会得出错误的结论。如果有必要得到正确的答案,从一个已知的源中用正确的时间检索当前时间。

        5
  •  3
  •   Hernán    11 年前

    Smalltalk中使用Pharo/Squeak的示例

    (Date year: 2014 month: 4 day: 24) = Date yesterday
    
        6
  •  2
  •   Artefacto    14 年前

    这接受一个可选的 DateTimeZone 对象。如果未给定,则使用当前设置的默认时区。

    <?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;
    }
    
        7
  •  2
  •   Iain Ward    14 年前

    另一个例子:

    bool isYesterday = DateTime.Now.Date.AddDays(-1) == dateToCheck.Date;
    
        8
  •  2
  •   jgauffin    14 年前

    代码:

    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;
        }
    }
    

    示例:

    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");
        }
    }
    
        9
  •  1
  •   Anurag    14 年前

    在JavaScript中,您可以编写

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

    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
        });
    })();
    
        10
  •  1
  •   chri3g91    7 年前

    C级#

        TimeSpan difference = DateTime.Now.Date - olderDate.Date;
        bool isYesterday = difference.TotalDays == 1;
    
        11
  •  -1
  •   rakuo15    14 年前

    您可以尝试一下此函数:

    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;
     }