SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: compare dates

  1. #1
    SitePoint Guru
    Join Date
    Mar 2006
    Posts
    701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    compare dates

    I have a date with format '2011-08-29 11:35:00'
    How can I check it with current time and date?I use prototype framework

  2. #2
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,286
    Mentioned
    181 Post(s)
    Tagged
    4 Thread(s)
    If you use the Javascript Date object you can simply compare with normal operators.
    Type "javascript compare date objects" into Google.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without any timezone information it will be an invalid date-
    but you can assume GMT and get something from it-

    Code:
    Date.fromISO= function(s){
    	var day, tz,
    	rx=/^(\d{4}\-\d\d\-\d\d([tT ][\d:\.]*)?)([zZ]|([+\-])(\d\d):(\d\d))?$/,
    	p= rx.exec(s) || [];
    	if(p[1]){
    		day= p[1].split(/\D/);
    		for(var i= 0, L= day.length; i<L; i++){
    			day[i]= parseInt(day[i], 10) || 0;
    		};
    		day[1]-= 1;
    		day= new Date(Date.UTC.apply(Date, day));
    		if(!day.getDate()) return NaN;
    		if(p[5]){
    			tz= (parseInt(p[5], 10)*60);
    			if(p[6]) tz+= parseInt(p[6], 10);
    			if(p[4]== '+') tz*= -1;
    			if(tz) day.setUTCMinutes(day.getUTCMinutes()+ tz);
    		}
    		return day;
    	}
    	return NaN;
    }
    Date.fromISO('2011-08-29 11:35:00').toUTCString()

    /* returned value: (String)
    Mon, 29 Aug 2011 11:35:00 GMT
    */


    var when=Date.fromISO('2011-08-29 11:35:00');
    if(Date.UTC(when)>Date.UTC(new Date()))alert('later');
    else alert('sooner');


Bookmarks

Posting Permissions

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