0

I'm using PHP, jQuery/JavaScript on my website.

For showing and selecting the date on a form in a "Date Control" I'm using one library titled jquery.dateLists.min.js

The issue I'm facing is with the time-zone on server is America/Los_Angeles but the script is setting the default today's date of the date control according to the time-zone Asia/Calcutta which is the time-zone set on my local machine.

How to set the today's date according to the time-zone set on server i.e. according to the time-zone America/Los_Angeles

Following is the HTML and script which sets the current date according to the time-zone Asia/Calcutta.

HTML code:

jQuery code to call the function :

$('#reg_date').dateDropDownsforRebate({dateFormat:'mm-dd-yy'});

The URL of the below library is as follows :

http://www.amdonnelly.co.uk/things/date-drop-down-lists.aspx

Javacript code from library file :

(function(a){a.fn.dateDropDowns=function(k){var g={dateFormat:"dd-mm-yy",monthNames:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],yearStart:"1914",yearEnd:"2000"},k=a.extend(g,k);return this.each(function(){function l(){var c=d.getMonth()+1,p="",f=1;_daysInMonth=m(c,d.getFullYear())+1;a("#"+e+"_list").children().length<_daysInMonth?f=a("#"+e+"_list").children().length+1:a("#"+e+"_list").children().remove();for(_x=f;_x<_daysInMonth;_x++)c=d.getDate()==_x?"selected='true'": "",p+="<option value='"+_x+"' "+c+">"+_x+"</option>";a("#"+e+"_list").append(p)}function m(c,a){var d=31;c==4||c==6||c==9||c==11?d=30:c==2&&(d=a%4==0?29:28);return d}function n(){var c=d.getDate(),a=d.getMonth()+1,f=d.getFullYear(),b=g.dateFormat;b.indexOf("DD")>-1&&c.toString().length<2&&(c="0"+c);b.indexOf("MM")>-1&&a.toString().length<2&&(a="0"+a);b=g.dateFormat.toLowerCase();b=b.replace("dd",c);b=b.replace("mm",a);b=b.replace("yy",f);j.val(b)}var j=a(this);j.html();var b=j.attr("id")+"_dateLists", e=b+"_day",h=b+"_month",i=b+"_year",k=j.val(),d=new Date,o=g.dateFormat.indexOf("/")>-1?"/":"-";(function(){if(k.length>0){var a=g.dateFormat.split(o),b=k.split(o),f=new Date;for(_x=0;_x<b.length;_x++)a[_x].toLowerCase().indexOf("d")>-1?f.setDate(b[_x]):a[_x].toLowerCase().indexOf("m")>-1?f.setMonth(b[_x]-1):a[_x].toLowerCase().indexOf("y")>-1&&f.setYear(b[_x]);d=f}})();(function(){var c=g.dateFormat.split(o),d=j;j.replaceWith("<div id='"+b+"' class='dateLists_container'></div>");for(_x=0;_x<c.length;_x++)c[_x].toLowerCase().indexOf("d")> -1?(a("#"+b).append("<div id='"+e+"' class='day_container'>"),a("#"+e).append("<select id='"+e+"_list' name='"+e+"_list' class='list'></select>"),a("#"+b).append("</div>")):c[_x].toLowerCase().indexOf("m")>-1?(a("#"+b).append("<div id='"+h+"' class='month_container'>"),a("#"+h).append("<select id='"+h+"_list' name='"+h+"_list' class='list'></select>"),a("#"+b).append("</div>")):c[_x].toLowerCase().indexOf("y")>-1&&(a("#"+b).append("<div id='"+i+"' class='year_container'>"),a("#"+i).append("<select id='"+ i+"_list' name='"+i+"_list' class='list'></select>"),a("#"+b).append("</div>"));a("#"+b).append(d);j.hide()})();l();(function(){a("#"+h+"_list").children().remove();for(_x=0;_x<12;_x++){var c=d.getMonth()==_x?"selected='true'":"";a("#"+h+"_list").append("<option value='"+_x+"' "+c+">"+g.monthNames[_x]+"</option>")}})();(function(){a("#"+i+"_list").children().remove();for(_x=parseInt(g.yearStart);_x<parseInt(g.yearEnd)+1;_x++){var c=d.getFullYear()==_x?"selected='true'":"";a("#"+i+"_list").append("<option value='"+ _x+"' "+c+">"+_x+"</option>")}})();(function(){a("#"+e+"_list").change(function(){d.setDate(a("#"+e+"_list").val());n()});a("#"+h+"_list").change(function(){var c=parseInt(a("#"+h+"_list").val()),b=d.getDate();_daysInMonth=m(c+1,d.getFullYear());b>_daysInMonth&&(b=_daysInMonth);d=new Date(d.getFullYear(),c,b,0,0,0,0);l();n()});a("#"+i+"_list").change(function(){var b=a("#"+i+"_list").val(),e=d.getDate(),f=d.getMonth();_daysInMonth=m(f+1,b);e>_daysInMonth&&(e=_daysInMonth);d=new Date(b,f,e,0,0,0,0); l();n()})})()})}})(jQuery);

(function(a){a.fn.dateDropDownsforRebate=function(k){var g={dateFormat:"dd-mm-yy",monthNames:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],yearStart:"2014",yearEnd:"2050"},k=a.extend(g,k);return this.each(function(){function l(){var c=d.getMonth()+1,p="",f=1;_daysInMonth=m(c,d.getFullYear())+1;a("#"+e+"_list").children().length<_daysInMonth?f=a("#"+e+"_list").children().length+1:a("#"+e+"_list").children().remove();for(_x=f;_x<_daysInMonth;_x++)c=d.getDate()==_x?"selected='true'": "",p+="<option value='"+_x+"' "+c+">"+_x+"</option>";a("#"+e+"_list").append(p)}function m(c,a){var d=31;c==4||c==6||c==9||c==11?d=30:c==2&&(d=a%4==0?29:28);return d}function n(){var c=d.getDate(),a=d.getMonth()+1,f=d.getFullYear(),b=g.dateFormat;b.indexOf("DD")>-1&&c.toString().length<2&&(c="0"+c);b.indexOf("MM")>-1&&a.toString().length<2&&(a="0"+a);b=g.dateFormat.toLowerCase();b=b.replace("dd",c);b=b.replace("mm",a);b=b.replace("yy",f);j.val(b)}var j=a(this);j.html();var b=j.attr("id")+"_dateLists", e=b+"_day",h=b+"_month",i=b+"_year",k=j.val(),d=new Date,o=g.dateFormat.indexOf("/")>-1?"/":"-";(function(){if(k.length>0){var a=g.dateFormat.split(o),b=k.split(o),f=new Date;for(_x=0;_x<b.length;_x++)a[_x].toLowerCase().indexOf("d")>-1?f.setDate(b[_x]):a[_x].toLowerCase().indexOf("m")>-1?f.setMonth(b[_x]-1):a[_x].toLowerCase().indexOf("y")>-1&&f.setYear(b[_x]);d=f}})();(function(){var c=g.dateFormat.split(o),d=j;j.replaceWith("<div id='"+b+"' class='dateLists_container'></div>");for(_x=0;_x<c.length;_x++)c[_x].toLowerCase().indexOf("d")> -1?(a("#"+b).append("<div id='"+e+"' class='day_container'>"),a("#"+e).append("<select id='"+e+"_list' name='"+e+"_list' class='list'></select>"),a("#"+b).append("</div>")):c[_x].toLowerCase().indexOf("m")>-1?(a("#"+b).append("<div id='"+h+"' class='month_container'>"),a("#"+h).append("<select id='"+h+"_list' name='"+h+"_list' class='list'></select>"),a("#"+b).append("</div>")):c[_x].toLowerCase().indexOf("y")>-1&&(a("#"+b).append("<div id='"+i+"' class='year_container'>"),a("#"+i).append("<select id='"+ i+"_list' name='"+i+"_list' class='list'></select>"),a("#"+b).append("</div>"));a("#"+b).append(d);j.hide()})();l();(function(){a("#"+h+"_list").children().remove();for(_x=0;_x<12;_x++){var c=d.getMonth()==_x?"selected='true'":"";a("#"+h+"_list").append("<option value='"+_x+"' "+c+">"+g.monthNames[_x]+"</option>")}})();(function(){a("#"+i+"_list").children().remove();for(_x=parseInt(g.yearStart);_x<parseInt(g.yearEnd)+1;_x++){var c=d.getFullYear()==_x?"selected='true'":"";a("#"+i+"_list").append("<option value='"+ _x+"' "+c+">"+_x+"</option>")}})();(function(){a("#"+e+"_list").change(function(){d.setDate(a("#"+e+"_list").val());n()});a("#"+h+"_list").change(function(){var c=parseInt(a("#"+h+"_list").val()),b=d.getDate();_daysInMonth=m(c+1,d.getFullYear());b>_daysInMonth&&(b=_daysInMonth);d=new Date(d.getFullYear(),c,b,0,0,0,0);l();n()});a("#"+i+"_list").change(function(){var b=a("#"+i+"_list").val(),e=d.getDate(),f=d.getMonth();_daysInMonth=m(f+1,b);e>_daysInMonth&&(e=_daysInMonth);d=new Date(b,f,e,0,0,0,0); l();n()})})()})}})(jQuery);

Please help me.

benomatis
  • 5,536
  • 7
  • 36
  • 59
PHPLover
  • 1
  • 51
  • 158
  • 311
  • please don't post the code of the library script, but a link where we can find the documentation – Mainz007 Dec 18 '14 at 07:41
  • @Mainz007 : Here is the URL where you will get the library I'm using :http://www.amdonnelly.co.uk/things/date-drop-down-lists.aspx – PHPLover Dec 18 '14 at 07:44
  • possible duplicate of [How to get server timezone in javascript](http://stackoverflow.com/questions/5742505/how-to-get-server-timezone-in-javascript) – benomatis Dec 18 '14 at 07:54
  • while loading that element(data drop down) fire one ajax call to server to get time and date of server.then set that values to element . – yugi Dec 18 '14 at 07:57

1 Answers1

1

JavaScript is a client based language. That means that there exists no communication between Server and JavaScript but only between your browser and JavaScript. So it can't access the given timezone from your server. It should always show the timezone your PC is in (and I guess that is Asia/Calcutta).

And looking at the plugin, I don't see a possability to set the timezone manually in the plugin itself.

Mainz007
  • 533
  • 5
  • 16
  • Thanks for your reply. But tell me one thing can I set the time zone as 'America/Los_Angeles' in the above code and set the today's date according to that time zone. So it will be irrespective of the location from where the site is being accessed. Can this be done? – PHPLover Dec 18 '14 at 07:53
  • Yes by the method yugi said in his comment or the [duplicate link](http://stackoverflow.com/a/5742576/2731055) from @webino has a good explanation – Mainz007 Dec 18 '14 at 08:04