So,
I'm reading this thread. And I'm thinking about your problem:
1. You need to store accurate timestamps
2. You need to *show* these timestamps to users in the user's local time
If storing a time stamp needs to be accurate, do not use the client's clock. Here's why-
JavaScript's Date() function already gets the local time. Run it and see for yourself:
Code:
> Date() "Sat Oct 11 2014 09:44:21 GMT-0400 (Eastern Standard Time)"
Code:
> Date() "Tue Jul 01 2014 08:47:38 GMT-0500 (Eastern Standard Time)"
If you want "more reliable" time stamps, you should collect time stamps server-side from the server clock. Not only that, but you probably should store the time normalized to UTC. If you don't store it as UTC, then you will need to store the timezone that was used by the server. Why? What if your server that stores your data moved from Virgina to California? The server's timezone has changed! And now all your time stamps gathered from the old server's clock in the database are incorrect relative to the new server! So, UTC is probably the best way to go.
This addresses what I believe to be your first requirement.
As for your second requirement, showing the time to the user...
If you store time in UTC on the server, then it's not much more work to get that time formatted client-side. Looking at an example here, assume I have the date "6/29/2011 4:52:48 PM" stored on the server. Send this time back to the client, append UTC - to indicate that it's a UTC time - and send it through Date():
Code:
> Date('6/29/2011 4:52:48 PM UTC') "Sat Oct 11 2014 11:48:52 GMT-0400 (Eastern Standard Time)"
If you want to format this time to display differently, then that's simple enough to do. Alpha extend the Date() object with 2 functions that I posted about a while ago here: http://msgboard.alphasoftware.com/al...ght=fromformat. They use the date & time format rules you see in the Grid & Dialog(UX?) for formatting the time. So, you could totally do something like this:
Code:
var format = "Weekday MM/dd/yyyy h:0m AM"; var time = new Date('6/29/2011 4:52:48 PM UTC'); // You must use new to create the date object alert(time.toFormat(format));
dateFormatted.png
This, I feel, addresses your second requirement.
AS A FINAL NOTE: If you are not timestamping and storing this stuff on a server, rather doing it all in some sort of client-side mobile app that never talks with anything else, then you can pretty much operate client-side and get time straight from the client. It's up to the user to be responsible and keep their clock accurate in this situation. But, IMHO, if you're storing time in a server and need accurate timestamps, I would never trust the client's clock for time stamp information ever.
Leave a comment: