Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Convert ISO 8601 Timestamp (90% solved)

    Greetings All,

    I'm trying to utilize prettyDate to convert my ISO 8601 strings from an XML document to a pretty conversion:
    (ie. 4 days ago)

    I currently have it working for this format:
    2012-01-28T20:24:17Z

    But it will not work with this format:
    2012-01-28T20:24:17.425Z

    I cannot change the data. The second option is how my data is stored.
    Is there any way to get prettyDate javascript to ignore the last 3 digits (.425) from the string? Or even more elegant, just convert them into the equation?

    Any help would be very appreciated.

    JavaScript
    Code:
    function pretty_date(date_str) {
        var time = ('' + date_str).replace(/-/g, "/").replace(/[TZ]/g, " ");
        var seconds = (new Date - new Date(time)) / 1000;
        var token = 'ago',
            list_choice = 1;
        if (seconds < 0) {
            seconds = Math.abs(seconds);
            token = 'from now';
            list_choice = 2;
        }
        var i = 0,
            format;
        while (format = time_formats[i++]) if (seconds < format[0]) {
            if (typeof format[2] == 'string') return format[list_choice];
            else return Math.floor(seconds / format[2]) + ' ' + format[1] + ' ' + token;
        }
        return time;
    };
    var time_formats = [
        [60, 'just now', 1], // 60 
        [120, '1 minute ago', '1 minute from now'], // 60*2
        [3600, 'minutes', 60], // 60*60, 60
        [7200, '1 hour ago', '1 hour from now'], // 60*60*2 
        [86400, 'hours', 3600], // 60*60*24, 60*60 
        [172800, 'yesterday', 'tomorrow'], // 60*60*24*2 
        [604800, 'days', 86400], // 60*60*24*7, 60*60*24 
        [1209600, 'last week', 'next week'], // 60*60*24*7*4*2 
        [2419200, 'weeks', 604800], // 60*60*24*7*4, 60*60*24*7 
        [4838400, 'last month', 'next month'], // 60*60*24*7*4*2 
        [29030400, 'months', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4 
        [58060800, 'last year', 'next year'], // 60*60*24*7*4*12*2 
        [2903040000, 'years', 29030400], // 60*60*24*7*4*12*100, 60*60*24*7*4*12 
        [5806080000, 'last century', 'next century'], // 60*60*24*7*4*12*100*2 
        [58060800000, 'centuries', 2903040000] // 60*60*24*7*4*12*100*20, 60*60*24*7*4*12*100
    ];
       $('#pretty').text(pretty_date($('#mydate').text()));
    HTML
    Code:
    <div id='mydate'>2012-01-28T20:24:17Z</div>
    <span id='pretty'></span>
    pretty.js
    Code:
    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 minute ago" ||
    			diff < 3600 && Math.floor( diff / 60 ) + " minutes 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 ) + " weeks 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 date = prettyDate(this.title);
    			if ( date )
    				jQuery(this).text( date );
    		});
    	};

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Well, I wouldn't have done it that way, at all.

    But you should be able to do it by just changing this line:
    Code:
    var time = ('' + date_str).replace(/-/g, "/").replace(/[TZ]/g, " ");
    to this
    Code:
    var time = ('' + date_str).replace(/-/g, "/").replace(/T/g, " ").replace(/(\.\d*)?Z$/,"");
    That says "replace either ".dddZ" or just "Z" with nothing if either one appears last in the string."
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    sinsysonline (03-26-2013)

  • #3
    New Coder
    Join Date
    Dec 2012
    Posts
    15
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up

    You are the man

    I was thinking about parsing it differently, and I know the code can be a tad sloppy, but I'm integrating several external XML / RSS feeds and their data is all sorts of misconstrued.

    Anyway, that worked perfectly and you deserve a round of applause.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •