1

i have a page where people post various issue. i am saving date and time in sql server which is not UTC date and time. our web server is running in USA pc. so tell me how to save date and time in such a so when people will see their posted issue then they see the posted date and time as per their local time.

i have a plan i can save date and time in UTC format in sql server and fetch that date and time in UTC format and convert that utc date and time to local date and time by c# but the problem is our server side logic will run in USA pc. so if i convert to local date and time then it will be USA local date and time.

so tell me what logic i should use at mvc server side as a result from where the user will see my page they will see date and time as per their local time.

i can not work with user time zone because user time zone related info never pass to server side from client pc.

mostly how popular forum like http://forums.asp.net, stackoverflow.com etc do this ?

forums render date and time from server side not client side. so what logic they use.

please guide me in detail to achieve this task. thanks

2 Answers2

2

Store all dates using UTC in the SQL server and also display them in UTC on the client. If you need to convert them and display to local timezone you will need to use javascript for that and convert the UTC date coming from the server to the local datetime. Here's an example: https://stackoverflow.com/a/6525602/29407

mostly how popular forum like http://forums.asp.net, stackoverflow.com etc do this ?

I am not sure about other sites, but on StackOverflow all dates shown in the UI are in UTC.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • you said `display them in UTC on the client` if i show the date and time in UTC format in the client then some confusion may occur. suppose my web server is in USA pc and one user from India post a question during day time but at the same time USA has night. so there could be date difference. suppose when date is 5/5/2015 then USA date could be 4/5/2015. so what UTC date will be flashed in page? –  Nov 28 '15 at 17:53
  • 1
    `if i show the date and time in UTC format in the client then some confusion may occur.` - no, that's exactly the whole point of UTC - to avoid any confusions. It doesn't matter where your server is as long as you store the date in UTC format in your database and display it in UTC to the client. But I repeat once again what I already said in my answer: if you have a requirement to display the date respecting the client timezone then simply convert the UTC date coming from your server to local datetime using javascript. – Darin Dimitrov Nov 28 '15 at 22:16
1

Have the server send down UTC, and have the client convert it to local time. Here's a specific solution from another thread: https://stackoverflow.com/a/53961671/534514

Mike S
  • 3,058
  • 1
  • 22
  • 12