Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:52:51+00:00 2026-05-29T19:52:51+00:00

I want to write a jQuery plugin that does: 2012-12-31 23:00:00 -> the following

  • 0

I want to write a jQuery plugin that does:

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

  • Tomorrow
  • Yesterday
  • Day after tomorrow
  • Day before tomorrow
  • Saturday
  • Previous Saturday
  • Next week Friday
  • Past week Monday
  • Friday 21 Januari

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]);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:52:53+00:00Added an answer on May 29, 2026 at 7:52 pm

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a word addin that does some computations and updates some
I'm searching for a jQuery plugin that does this. for example: <label><input type=checkbox depends_on=foo=5
i'm trying to write a jquery plugin that when i click on a image
For a JQuery plugin that I am trying to write I would like to
I want to know how to correctly write an if statement with jquery. I
I want to read multiple RSS feeds using jQuery. I'm trying to write a
before this I wrote all jquery-code into main fail. Now I want to move
I want to write a command that specifies the word under the cursor in
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.