Skip to content

Instantly share code, notes, and snippets.

@raulmoyareyes
Last active March 2, 2018 14:32
Show Gist options
  • Save raulmoyareyes/ba8cf8adc5f1a65e8377 to your computer and use it in GitHub Desktop.
Save raulmoyareyes/ba8cf8adc5f1a65e8377 to your computer and use it in GitHub Desktop.
Hora con horario de verano en Javascript
//Cambio de UTC+1 a UTC+2 en verano.
//El cambio horario se produce (a nivel Europeo) el último domingo de Marzo y de Octubre
function lastSunday(){
//días que tiene un mes
var nD = new Date(new Date().getFullYear(), new Date().getUTCMonth(), 0).getUTCDate();
var d = new Date().getUTCDay();
var n = new Date().getUTCDate();
var j = n;
// calculo el día del siguiente domingo
if (d !== 6){ j = (6-d+1)+n; }
//bucar el ultimo domingo
var i = 0;
var sun = 0;
for(i=j; i <= nD; i=i+7){sun = i;}
return sun;
}
function DST(today){
var month = today.getUTCMonth();
var day = today.getUTCDay();
if (month >= 2 && month <= 9){
if((month === 2 && day >= lastSunday()) || (month === 9 && day <= lastSunday()) ){
return 2;
}else if(month > 2 && month < 9){
return 2;
}
}
return 1;
}
var today = new Date();
var dayW = today.getUTCDay(); //dia de la semana 0-6
var hour = today.getUTCHours(); //hora 0-23
var minutes = today.getUTCMinutes(); // minutos 0-59
var p = this.DST(today);
if (hour+p >= 24) {
hour = (hour+p)%24;
if (dayW+1 === 7){
dayW = 0;
} else {
dayW += 1;
}
} else {
hour += p;
}
console.log(hour+":"+minutes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment