Friday, September 16, 2011

Javascript --> Local time conversion for UTC conversion without Timezone

<script>
function date_convert(date){
sdate=date;
var tarr=sdate.split(' ');
var tarr1=tarr[1].split(':');
if(tarr1[0]!='00'){
while(tarr1[0].charAt(0) == '0')
tarr1[0]= tarr1[0].substr(1);
var t_hr1=parseInt(tarr1[0]);
var hour_min=(t_hr1*60)+parseInt(tarr1[1]);
}
else{
var hour_min=parseInt(tarr1[1]);
}
var tz=new Date().getTimezoneOffset();
var tdiff=hour_min+parseInt(tz);
if(tdiff<0){
var t_hr=24*60;
tdiff=t_hr+tdiff;
var d_arr=tarr[0].split('-');
dt=d_arr[2];
mt=d_arr[1]-1;
yt=d_arr[0];
var o=new Date(yt,mt,dt);
var p_date=new Date(o.getFullYear(), o.getMonth(), o.getDate() - 1);
var py=p_date.getFullYear();
var pm=p_date.getMonth()+1;
var pd=p_date.getDate();
if(parseInt(pm)<10)
pm='0'+pm;
if(parseInt(pd)<10)
pd='0'+pd;
//var d_date=parseInt(d_arr[2])-1;
}
else{
var d_arr=tarr[0].split('-');
var py=d_arr[0];
var pm=d_arr[1];
var pd=d_arr[2];
}
var time_hr=Math.floor(tdiff/60);
var time_min=tdiff%60;
if(parseInt(time_hr)<10)
time_hr='0'+time_hr;
if(parseInt(time_min)<10)
time_min='0'+time_min;
var date_time=py+'-'+pm+'-'+pd+' '+time_hr+':'+time_min+':'+tarr1[2];
return date_time;
}

var localtime_for_equal_utc_without_add_time_zone=date_convert('2011-09-16 05:40:00');
</script>

No comments:

Post a Comment