display a day and time in localized timezone, in text, a fixed day/time |
display a day and time in localized timezone, in text, a fixed day/time |
bluffwood |
Dec 31 2016, 07:33 AM
Post
#1
|
Group: Members Posts: 6 Joined: 31-December 16 Member No.: 24,986 |
I produce radio shows, that are streamed on multiple outlets at various times. I'd like to list of schedule of when people can tune in. These are not podcasts but live feeds of streaming radio stations.
For example, my show runs 5 times on the weekend. Saturday Noon CT on station A Sunday 7am MT on station B Sunday Noon MT on station C Sunday 9pm ET on Station D Sunday 9pm MT on Station E It's the same times week to week, so I don't want a date, just a day and time. I'd like the user to see these fixed days/times in their local timezone All of the streaming stations (so far) participate in Daylight saving time (US/Northern hemisphere) but that won't likely be the case for much longer. It would be nice if the displays of this schedule took DST of the source and the local listener into account. So if the viewer is in Eastern Time, it would read: Saturday 1pm ET on station A Sunday 9am ET on station B Sunday 2pm ET on station C Sunday 9pm ET on Station D Sunday 11pm ET on Station E and a viewer in Pacific time would read: Saturday 10am PT on station A Sunday 6am PT on station B Sunday 11am PT on station C Sunday 6pm PT on Station D Sunday 8pm PT on Station E this will just display in a cell in an html table. Thanks, in advance, for your help. |
Christian J |
Dec 31 2016, 08:04 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,628 Joined: 10-August 06 Member No.: 7 |
With a server-side script (e.g. PHP) you can get the time set on the radio show's web server. Next, with javascript you can get the time set on the user's own computer, and by adding/subtracting those two time values find out the time difference.
|
bluffwood |
Dec 31 2016, 09:18 PM
Post
#3
|
Group: Members Posts: 6 Joined: 31-December 16 Member No.: 24,986 |
With a server-side script (e.g. PHP) you can get the time set on the radio show's web server. Next, with javascript you can get the time set on the user's own computer, and by adding/subtracting those two time values find out the time difference. it's much simpler than that. I'll hand code the day/time in whatever form a script can convert them to "user time" just need a real simple script. |
pandy |
Jan 1 2017, 11:54 AM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,716 Joined: 9-August 06 Member No.: 6 |
Will JavaScript suffice?
If I've understood this half right it should be really simple, but it isn't. This is what we SHOULD be able to do, take a UTC time and convert it to local time, but it doesn't work in (most?) browsers. CODE var airTime = new Date('2016-01-01 17:00:00 UTC'); var localAirTime = airTime.toLocaleString(); alert(localAirTime); toString() automatially converts to local date but JavaScript chokes on the date format. Because of this problem there's a lot of JavaScript time libraries around. You can google for that. I discovered that it works just fine if you convert the date and time to epoch time. https://en.wikipedia.org/wiki/Unix_time You do that with getTime(). Then you can get it back to a normal time format in different ways. You want the user's local date and time, so you could either use toString() which will produce the standard format or you could use toLocaleString() which will produce the format the user has chosen for his machine. Note that you need to use UTC and not your local time for the airing times! CODE var airTimeUTC = new Date("January 1, 2017 17:00:00 UTC"); var airTimeEpoch = airTimeUTC.getTime(); var localAirTime = new Date(airTimeEpoch); alert(localAirTime.toString() + '\n' + localAirTime.toLocaleString()); I set the time to the next whole hour (at the time of writing). I'm in Sweden (GMT +1) and I get the below result. The second line is my preferred format and the first the standard one. If you want to format it another way you can do so by picking the time and date apart with getHours(), getMinutes() and so on. CODE Sun Jan 01 2017 18:00:00 GMT+0100 (Romance Standard Time) 2017-01-01 18:00:00 If you want to use this for something else, note that epoch time is in seconds, but JavaScript uses milliseconds for some reason, so often one needs to calculate with a factor 1000, but that isn't needed here. Also, when I needed this I googled long and hard and didn't find this solution anywhere. I can't think I'm the only one who has thought of this since it's so very simple, so there may be something fishy with it. On the other hand, then I should have found something about the fishiness but I didn't do that either. Anyway, it has worked for me. |
bluffwood |
Jan 2 2017, 03:17 PM
Post
#5
|
Group: Members Posts: 6 Joined: 31-December 16 Member No.: 24,986 |
I'm thinking it looks elegant, and might well do.
If I may bore you with a little background. I was a developer in the early 80s, and responsible for what became the ping command on three platforms.... this was before the invention of the pc, and before the announcement of the Internet. It was written in "c" in a version developed by the university of Waterloo (Ontario, Canada). C was so new, I think there was only one book, by the developers Kernihan & Ritchie. I had been a "b" programmer. The very first "C" compiler was written in "b" so much for the ancient history. Now my request, because I'm really quite stumped still. Can this script be made into a function that is callable, giving it the time of day in UTC and getting the time back as a text string, in AM/PM format? I know you're probably thinking, "I did almost all that already!" and yes, wow, you found a solution to a problem which would be quite handy to many webpage developers, that no one ever thought of solving. The fact it doesn't work (at first) on all browsers or worse, if it works differently on browsers, that's the typically nutty world of browsers I've encountered again and again. Your continued assistance would be gratefully received, as I'm long out of the world of developers. Will JavaScript suffice? If I've understood this half right it should be really simple, but it isn't. This is what we SHOULD be able to do, take a UTC time and convert it to local time, but it doesn't work in (most?) browsers. CODE var airTime = new Date('2016-01-01 17:00:00 UTC'); var localAirTime = airTime.toLocaleString(); alert(localAirTime); toString() automatially converts to local date but JavaScript chokes on the date format. Because of this problem there's a lot of JavaScript time libraries around. You can google for that. I discovered that it works just fine if you convert the date and time to epoch time. https://en.wikipedia.org/wiki/Unix_time You do that with getTime(). Then you can get it back to a normal time format in different ways. You want the user's local date and time, so you could either use toString() which will produce the standard format or you could use toLocaleString() which will produce the format the user has chosen for his machine. Note that you need to use UTC and not your local time for the airing times! CODE var airTimeUTC = new Date("January 1, 2017 17:00:00 UTC"); var airTimeEpoch = airTimeUTC.getTime() var localAirTime = new Date(airTimeEpoch); alert(localAirTime.toString() + '\n' + localAirTime.toLocaleString()); I set the time to the next whole hour (at the time of writing). I'm in Sweden (GMT +1) and I get the below result. The second line is my preferred format and the first the standard one. If you want to format it another way you can do so by picking the time and date apart with getHours(), getMinutes() and so on. CODE Sun Jan 01 2017 18:00:00 GMT+0100 (Romance Standard Time) 2017-01-01 18:00:00 If you want to use this for something else, note that epoch time is in seconds, but JavaScript uses milliseconds for some reason, so often one needs to calculate with a factor 1000, but that isn't needed here. Also, when I needed this I googled long and hard and didn't find this solution anywhere. I can't think I'm the only one who has thought of this since it's so very simple, so there may be something fishy with it. On the other hand, then I should have found something about the fishiness but I didn't do that either. Anyway, it has worked for me. |
pandy |
Jan 2 2017, 05:20 PM
Post
#6
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,716 Joined: 9-August 06 Member No.: 6 |
Yeah, that can be done. But do you want them to click a link? I thought you wanted the times displayed on the page to start with? Either way can be done.
And when they click, what should happen? A popup with the time table? The times displayed somewhere on the page? The format would be AM/PM if that's what's used where the user lives. Here we use a 24 hour clock so that's what I get. It's all happening on the user's machine. Don't you get a a AM/PM time if you runt the script? I'm off googling that "B"... Is B the reason C is called C BTW? |
Lo-Fi Version | Time is now: 19th March 2024 - 05:29 AM |