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 8593769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:03:53+00:00 2026-06-12T00:03:53+00:00

I found this snippet at John Resig’s blog: function prettyDate(time){ var date = new

  • 0

I found this snippet at John Resig’s blog:

function prettyDate(time){
    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
        diff = (((new Date()).getTime() - date.getTime()) / 1000),
        day_diff = Math.floor(diff / 86400);

    if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
        return;

    return day_diff == 0 && (
            diff < 60 && "just now" ||
            diff < 120 && "1 min" ||
            diff < 3600 && Math.floor( diff / 60 ) + " mins" ||
            diff < 7200 && "1 hour" ||
            diff < 86400 && Math.floor( diff / 3600 ) + " hours") ||
        day_diff == 1 && "Yesterday" ||
        day_diff < 7 && day_diff + " d" ||
        day_diff < 31 && Math.ceil( day_diff / 7 ) + " w";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
    jQuery.fn.prettyDate = function(){
        return this.each(function(){
            var date = prettyDate(this.title);
            if ( date )
                jQuery(this).text( date );
        });
    };

My timezone on server is UTC, I am not sure what timezone will this code work on?

In my html i render my time as follows:

<span id="p-date">2012-09-26T00:12:15</span>

Will doing

  $(function() {
  $("#p-date").prettyDate();
  setInterval(function(){ $("#p-date").prettyDate(); }, 5000);
  });

humanize the time?

  • 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-06-12T00:03:54+00:00Added an answer on June 12, 2026 at 12:03 am

    The above snippet does not take care of timezone. In case the server has UTC timezone, you will need to do an extra (d.getTimezoneOffset()*60000) to convert to local time.

    The entire function below:

    function prettyDate(time){
        d = new Date();
        var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
            diff = ((d.getTime() + (d.getTimezoneOffset()*60000) - date.getTime()) / 1000),
            day_diff = Math.floor(diff / 86400);
         if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
            return;
    
        return day_diff == 0 && (
                diff < 60 && "just now" ||
                diff < 120 && "1 min ago" ||
                diff < 3600 && Math.floor( diff / 60 ) + " mins ago" ||
                diff < 7200 && "1 hour ago" ||
                diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
            day_diff == 1 && "Yesterday" ||
            day_diff < 7 && day_diff + " days ago" ||
            day_diff < 31 && Math.ceil( day_diff / 7 ) + " week ago";
    }
    
    // If jQuery is included in the page, adds a jQuery plugin to handle it as well
    if ( typeof jQuery != "undefined" )
        jQuery.fn.prettyDate = function(){
            return this.each(function(){
                var $this = jQuery(this),  
                    date = prettyDate($this.text());
                if ( date )
                    $this.text( date );
            });
        };
    
    $(function() {
        $(".p-date").prettyDate();
        setInterval(function(){ $(".p-date").prettyDate(); }, 5000);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this snippet: (function( cn, url ) { if( navigator.cookieEnabled && !new RegExp(
I'm reading Programming Perl , and I found this code snippet: sub new {
I found this snippet of code in my travels in researching JSON: var array
I found this snippet: def timesince(dt, default=just now): now = datetime.utcnow() diff = now
I have found this snippet in html5: <script> function drag(target, evt) { evt.dataTransfer.setData("Text", target.id);
I found this snippet to change the status line color when i go to
I found this snippet of code I am having trouble trying to find a
I've found this snippet of code on the net. It sets up an NSMutableArray
I'm trying to produce random integers (uniformly distributed). I found this snippet on an
I found this code snippet to retrieve the title from a YouTube video and

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.