jQuery相对日期

时间:2012-02-11 14:59:40

标签: jquery date

我想写一个jQuery插件:

2012-12-31 23:00:00 - >以下格式:

  • 明天
  • 昨天
  • 后天
  • 前天
  • 星期六
  • 上周六
  • 下周五。
  • 上周一
  • 周一21 Januari

我怎样才能以最好的方式做到这一点?我在jQuery中写这篇文章的经验不多。但是,我用PHP编写了这个类,但是我希望转换整个日期 - >相对日期进展到客户端,因为它是视图逻辑。

<?php

    class RelativeDate{
        private $unix;

        private $hour = 0;
        private $minute = 0;
        private $second = 0;
        private $month;
        private $day;
        private $year;
        private $weekNr = 0;

        public function setDate($day, $month, $year){
            $this->day = $day;
            $this->month = $month;
            $this->year = $year;
        }

        public function setTime($hour, $minute, $second){
            $this->hour = $hour;
            $this->minute = $minute;
            $this->second = $second;
        }

        public function setFromMysqlDateTime($input){
            $explode = explode(' ', $input);
            $this->setDateFromExplode($explode[0], 2, 1, 0);
            $this->setTimeFromExplode($explode[1], 1, 0);
        }

        public function setDateFromExplode($input, $dayPos, $monthPos, $yearPos, $separator = '-'){
            $explode = explode($separator, $input);
            $this->day = $explode[$dayPos];
            $this->month = $explode[$monthPos];
            $this->year = $explode[$yearPos];
        }

        public function setTimeFromExplode($input, $hourPos, $minPos, $secPos = null, $separator = ':'){
            $explode = explode($separator, $input);
            $this->hour = $explode[$hourPos];
            $this->minute = $explode[$minPos];

            if($secPos !== null){
                $this->second = $explode[$secPos];
            }
        }

        public function makeUnix(){
            $this->unix = mktime($this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year);
            $this->weekNr = (int)date('W', $this->unix);
        }

        public function getUnix(){
            return $this->unix;
        }

        public function isInFuture($buffer){
            if($this->unix >= strtotime($buffer)){
                return true;
            }

            return false;
        }

        public function getMongoDate(){
            return new MongoDate(strtotime($this->year . '-' . $this->month . '-' . $this->day . ' ' . $this->hour . ':' . $this->minute . ':' . $this->second));
        }

        public function getMysqlDate(){
            return $this->year . '-' . $this->month . '-' . $this->day;
        }

        public function isToday(){
            if(mktime(0, 0, 0, $this->month, $this->day, $this->year) === mktime(0, 0, 0, date('m')  , date('d'), date('Y'))){
                return true;
            }

            return false;
        }

        public function isTomorrow(){
            if(mktime(0, 0, 0, $this->month, $this->day, $this->year) === mktime(0, 0, 0, date('m')  , date('d') + 1, date('Y'))){
                return true;
            }

            return false;
        }

        public function isDayAfterTomorrow(){
            if(mktime(0, 0, 0, $this->month, $this->day, $this->year) === mktime(0, 0, 0, date('m')  , date('d') + 2, date('Y'))){
                return true;
            }

            return false;
        }


        public function isYesterday(){
            if(mktime(0, 0, 0, $this->month, $this->day, $this->year) === mktime(0, 0, 0, date('m')  , date('d') - 1, date('Y'))){
                return true;
            }

            return false;
        }

        public function isDayBeforeYesterday(){
            if(mktime(0, 0, 0, $this->month, $this->day, $this->year) === mktime(0, 0, 0, date('m')  , date('d') - 2, date('Y'))){
                return true;
            }

            return false;
        }

        public function isAfter($compareDate){
            if($this->unix > $compareDate->getUnix()){
                return true;
            }

            return false;
        }

        public function isInNextWeek(){
            if($this->weekNr === (int)(date('W') + 1)){
                return true;
            }
        }

        public function getDay(){
            $str = $this->day;

            // Let's not add any zero's.
            if(strlen($str) === 2){
                if($str[0] === '0'){
                    $str = $str[1];
                }
            }

            return $str;
        }

        public function getYear(){
            return $this->year;
        }

        public function isInPrevWeek(){
            if($this->weekNr === (int)(date('W') - 1)){
                return true;
            }
        }

        public function isInCurrentWeek(){
            if($this->weekNr === (int)date('W')){
                return true;
            }
        }

        public function dayToString($lang = 'nl'){
            $dayString = date('N', $this->unix);

            switch($dayString){
                case 1:
                    return 'Maandag';
                case 2:
                    return 'Dinsdag';
                case 3:
                    return 'Woensdag';
                case 4:
                    return 'Donderdag';
                case 5:
                    return 'Vrijdag';
                case 6:
                    return 'Zaterdag';
                case 7:
                    return 'Zondag';
            }
        }

        public function getMonthString($lang = 'nl'){
            switch($this->month){
                case 1:
                    return 'Januari';
                case 2:
                    return 'Februari';
                case 3:
                    return 'Maart';
                case 4:
                    return 'April';
                case 5:
                    return 'Mei';
                case 6:
                    return 'Juni';
                case 7:
                    return 'Juli';
                case 8:
                    return 'Augustus';
                case 9:
                    return 'September';
                case 10:
                    return 'Oktober';
                case 11:
                    return 'November';
                case 12:
                    return 'December';
            }
        }

        public function isInCurrentYear(){
            if($this->year !== date('Y')){
                return false;
            }

            return true;
        }
    }

// File end.

上面是我的PHP relativeDate类。以下是我到目前为止所得到的:

function relativeDate(){
    var _date;

    this.getDate = function(){
        return _date;
    }

    this.fromMysql = function(input){
        var regex = /^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
        var parts = input.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
        _date = new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]);
    }
}

1 个答案:

答案 0 :(得分:1)

首先阅读this并编写一些您需要的方法,例如 getTomorrow()。 然后尝试创建这样的对象。您创建一个具有所有可能日期名称的对象:

var date = new Date();
var dayNames = {};
dayNames[getTomorrowDate()] = "Tomorrow";
dayNames[getYesterdayDate()] = "Yesterday";
...

dayNames [getNextWeekDate()] =&#34; NextWeek&#34; + GetDayName();

现在,你可以找到这样的给定日期的正确名称:

dayNames[givenDate];// while givenDate is a string.