randomKek
randomKek

Reputation: 1128

jQuery relative dates

I want to write a jQuery plugin that does:

2012-12-31 23:00:00 -> the following formats:

How can I do this the best way? I have little experience writing this in jQuery. However, I have written this class in PHP, but I want to convert the whole date -> relative date progress to the clientside since it is view logic.

<?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.

Above is my PHP relativeDate class. Below is what I got so far:

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

Upvotes: 1

Views: 1210

Answers (1)

Reza Owliaei
Reza Owliaei

Reputation: 3363

Read this first and write some methods you need like getTomorrow(). Then try to create an object like this. You create an object with all possible dates name:

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

dayNames[getNextWeekDate()] = "NextWeek "+ GetDayName();

now, you can find the proper name of given date like this:

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

Upvotes: 1

Related Questions