Reputation: 65962
I'm working on a personal project involving Javascript, and as part of that project, I want to grab the current date (including time) and display it accordingly. No big deal right? Well, the deal is that I want to return the time & date in Eastern Daylight TIme, no matter where in the world the IP is.
If this is not possible, what alternative methods do you suggest? Does php have this functionality? I could write a simple php script that takes a date and converts it, but I want to keep this in JS if at all possible.
I'm trying to think through the best way to do this, but I'd appreciate any help that you could offer.
Thanks!
Upvotes: 6
Views: 18804
Reputation: 79
Checking if a given date is on the Easter days. If no date is given the current Date is used. 🙂
// If dateObject == null, current Datetime will be used.
// This function uses the "Meeus/Jones/Butcher" algorithm
function isEaster(dateObject) {
const date = dateObject ?? new Date();
const year = date.getFullYear();
// Calculate the date of good friday for the current year
const a = year % 19;
const b = Math.floor(year / 100);
const c = year % 100;
const d = Math.floor(b / 4);
const e = b % 4;
const f = Math.floor((b + 8) / 25);
const g = Math.floor((b - f + 1) / 3);
const h = (19 * a + b - d - g + 15) % 30;
const i = Math.floor(c / 4);
const k = c % 4;
const l = (32 + 2 * e + 2 * i - h - k) % 7;
const m = Math.floor((a + 11 * h + 22 * l) / 451);
const n = Math.floor((h + l - 7 * m + 114) / 31) - 1;
const p = (h + l - 7 * m + 114) % 31;
// Calculate the easter dates
const maundyThursday = new Date(year, n, p - 1);
const goodFriday = new Date(year, n, p);
const holySaturday = new Date(year, n, p + 1);
const easterSunday = new Date(year, n, p + 2);
const easterMonday = new Date(year, n, p + 3);
return ([maundyThursday, goodFriday, holySaturday, easterSunday, easterMonday].map(elem => {
return elem.toISOString().split('T')[0];
}).indexOf(date.toISOString().split('T')[0]) > -1);
}
Upvotes: 0
Reputation: 536509
JavaScript native Date
objects only know two timezones, UTC and the user's locale timezone (and even then, the amount of information you can extract about the locale timezone is limited). You could work in UTC and subtract 4 hours to get EDT, but do you really always want EDT and not EST?
If you want to do timezone conversions between arbitrary regions in PHP you'll need to drag in a large library with its own timezone information, such as TimezoneJS.
It may be better to keep the JavaScript stuff all in UTC, and let the PHP side worry about formatting it for a particular locale/timezone, using eg the timezone stuff from Date/Time.
Upvotes: 1
Reputation: 22438
I found this on the internet, and there are a lot more of these scripts:
function calcTime(offset) {
// create Date object for current location
d = new Date();
// convert to msec
// add local time zone offset
// get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
return new Date(utc + (3600000*offset));
}
So, you get the current time, add the offset of the current location to get UTC time and then you return a new date where you add the offset of a certain time zone again.
Upvotes: 7