Line 2,609: | Line 2,609: | ||
} | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | //if there is a time zone converter | ||
+ | if($("#timezone_converter").length){ | ||
+ | //global variables | ||
+ | var hold_all_dates = []; | ||
+ | var date_counter = 0; | ||
+ | |||
+ | |||
+ | //grab original dates | ||
+ | var grab_date; | ||
+ | var date_array; | ||
+ | |||
+ | //change dates to Boston timezone and collect all dates into a global variable | ||
+ | $('.date_timezone').each(function(i, obj) { | ||
+ | |||
+ | //get the date | ||
+ | grab_date = $(this).html(); | ||
+ | |||
+ | //change date to a number array | ||
+ | date_array = grab_date.split(',').map(Number); | ||
+ | |||
+ | //create an id for that specific date, we will use this later to append | ||
+ | $(this).attr('id', 'dt_'+date_counter); | ||
+ | |||
+ | //add to global variables | ||
+ | hold_all_dates[date_counter]=date_array; | ||
+ | date_counter++; | ||
+ | |||
+ | //clean the div and have it ready for the change | ||
+ | $(this).empty(); | ||
+ | |||
+ | //now append that date in Boston time: | ||
+ | $(this).append(change_time_zone(date_array[0],date_array[1],date_array[2],date_array[3],0)); | ||
+ | |||
+ | }); | ||
+ | |||
+ | |||
+ | //setup timezone select | ||
+ | var select_is_ready = false; | ||
+ | |||
+ | // DAYLIGHT SAVINGS TIME | ||
+ | select_is_ready = time_zone_dropdown('winter'); | ||
+ | |||
+ | //once timezone is ready, allow user to change time zones | ||
+ | if(select_is_ready){ | ||
+ | //if the user wants to change the timezone | ||
+ | $("#timezone_converter").on("change", function(e) { | ||
+ | |||
+ | //get the timezone requested | ||
+ | var timezone_selected = $(this).val(); | ||
+ | var time_difference = parseInt(timezone_selected); | ||
+ | |||
+ | |||
+ | //empty all timezones getting ready for appending | ||
+ | $('.date_timezone').empty(); | ||
+ | |||
+ | //get each date_timezone by number id | ||
+ | for(x=0; x< date_counter; x++){ | ||
+ | |||
+ | $('#dt_'+x).append(change_time_zone(hold_all_dates[x][0], | ||
+ | hold_all_dates[x][1], | ||
+ | hold_all_dates[x][2], | ||
+ | hold_all_dates[x][3], | ||
+ | time_difference)); | ||
+ | |||
+ | } | ||
+ | |||
+ | }); | ||
+ | |||
+ | } | ||
+ | else{ | ||
+ | console.log('error with appending timezone select'); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | //////////////////////////////////////////////////////////// | ||
+ | //TIME ZONE CONVERTER | ||
+ | |||
+ | |||
//time zone functions | //time zone functions | ||
//main function for switching time | //main function for switching time | ||
function change_time_zone(month, day, time, duration, change){ | function change_time_zone(month, day, time, duration, change){ | ||
− | + | //holds days per month | |
− | + | const days_in_month = [31, 28, 31, 30, | |
31, 30, 31, 31, | 31, 30, 31, 31, | ||
30, 30, 30, 31]; | 30, 30, 30, 31]; | ||
− | + | //maximum amount of days in the month we are in | |
− | + | var months_days_limit = days_in_month[month]; | |
− | + | //time event starts | |
− | + | var time_start = time + change; | |
− | + | //convert duration minutes in military format | |
− | + | if(duration%60 == 0){ | |
− | + | duration = (duration/60)*100; | |
− | + | } | |
− | + | else if( duration > 60){ | |
− | + | duration = (((duration / 60)|0)*100) + (duration % 60); | |
− | + | } | |
− | + | //add converted minutes | |
− | + | var time_end = time_start + duration; | |
− | + | //check if time_end needs to be adjusted | |
− | + | var time_end_minutes = time_end % 100; | |
− | + | if(time_end_minutes == 60){ | |
− | + | time_end = (time_end -60)+100; | |
− | + | } | |
− | + | else if(time_end_minutes > 60){ | |
− | + | //from those minutes, get how many hours | |
− | + | var sixty = ((time_end_minutes / 60)|0)*100; | |
− | + | ||
− | + | //add hours plus minutes in military format | |
− | + | time_end_minutes = sixty + ((time_end % 100) - 60); | |
− | + | //get the final time end by adding the correct minute convertion | |
− | + | time_end = (time_start - (time_start % 100)) + time_end_minutes; | |
− | + | } | |
− | + | //check if adjusted time change crosses the international date line | |
− | + | if(time_start >= 2400){ | |
− | + | ||
+ | time_start = time_start - 2400; | ||
+ | time_end = time_end - 2400; | ||
− | + | day = day + 1; | |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | if(time_start < 0){ | |
− | + | time_start = 2400 - time_start; | |
− | + | time_end = 2400 - time_end; | |
− | day = day - 1; | + | day = day - 1; |
+ | } | ||
+ | |||
+ | //check if the new day switches months | ||
+ | if(day > months_days_limit ){ | ||
+ | month = month + 1; | ||
+ | day = day - months_days_limit; | ||
+ | } | ||
+ | if(day <= 0 ){ | ||
+ | month = month - 1; | ||
+ | day = days_in_a_month[month]; | ||
+ | } | ||
− | + | //style date and time | |
− | + | var date_changed = month_day_for_display(month, day)+", " | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+hour_for_display(time_start)+" - " | +hour_for_display(time_start)+" - " | ||
+hour_for_display(time_end); | +hour_for_display(time_end); | ||
− | + | //return date changed and styled | |
− | + | return date_changed; | |
− | + | } | |
//switch hours from 24 to 12 format | //switch hours from 24 to 12 format | ||
Line 2,777: | Line 2,858: | ||
return (hour_obj.hours+":"+hour_obj.minutes+" "+hour_obj.midday); | return (hour_obj.hours+":"+hour_obj.minutes+" "+hour_obj.midday); | ||
} | } | ||
+ | |||
//append the values for the time zone select dropdown | //append the values for the time zone select dropdown | ||
Line 2,812: | Line 2,894: | ||
timezone_values_str = timezone_values_str+ | timezone_values_str = timezone_values_str+ | ||
− | "<option id='"+ utcs_ids[x] +"' value='"+ utc_value +"'>"+ | + | "<option id='"+ utcs_ids[x] + |
+ | "' value='"+ utc_value +"'>"+ | ||
utcs_names[x] + "</option>"; | utcs_names[x] + "</option>"; | ||
Line 2,820: | Line 2,903: | ||
//append the options | //append the options | ||
− | $("# | + | $("#timezone_converter").append(timezone_values_str); |
//its ready! | //its ready! | ||
return true; | return true; | ||
} | } | ||
+ | |||
Revision as of 16:51, 22 April 2021