1

I have a DateTime column field in Sql which is storing Values as UTC. Now I have a requirement where in my reports I have to convert the UTC into Local Time as per the geographical location from where User is accessing the report.

Now I know that ToLocalTime() will convert it into required but at the same time it will convert to the place where application is deployed i.e Server not the User's Geo location .

Here is my code snippet ..

<td>@string.Format("{0:yyyy-MMM-dd HH:mm:ss}", user.StatusDateTime.ToLocalTime())</td>

I am using Razor View and javascript/jquery in User interface.
What would be the possible solution ?

Arghya C
  • 9,805
  • 2
  • 47
  • 66
Lara
  • 2,821
  • 7
  • 39
  • 72
  • What have you tried and in which of the languages that you have tagged the question in? – Xotic750 Dec 30 '15 at 12:10
  • @Xotic750 The Datetime needs to be displayed into jquery Datatable.And as i am using Razor , so inside , used the ToLocalTime() snippet , Pls see the updated post. – Lara Dec 30 '15 at 12:13
  • http://stackoverflow.com/questions/6525538/convert-utc-date-time-to-local-date-time-using-javascript – Arghya C Dec 30 '15 at 12:16
  • @ArghyaC Okay.I know How to convert To Local Time in Javascript but here the Page is Razor , and i am directly getting the values from Server into datatable column – Lara Dec 30 '15 at 12:20

1 Answers1

1

Okay, what you can do is, pass the date to the view (without .ToLocalTime()), and then use JavaScript to render local date as shown in the link.

<script>document.write(new Date('@user.StatusDateTime.ToString("yyyy-MM-dd hh:mm:ss UTC")').toString());</script>

Notice the .ToString("yyyy-MM-dd hh:mm:ss UTC") to satisfy JavaScript (I guess there are better ways).

Edit

If you want to show the date in some specific format (e.g. 2015-Dec-22 12:11:38), you can use a custom JavaScript function. Here is an example.

function getFormattedLocalDate(utcDateString) {
    var date = new Date(utcDateString);
    var months = [
      "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    ];
    var formattedDate = date.getFullYear() + '-' + months[date.getMonth()] + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
    return formattedDate;
}

See it live.

Edit 2

Sample use inside html table here.

Arghya C
  • 9,805
  • 2
  • 47
  • 66
  • So this javascript code will convert the Server UTC datetime into User's geo location datetime ? – Lara Dec 30 '15 at 13:14
  • Why don't you try it out? :) – Arghya C Dec 30 '15 at 13:16
  • Thanks a lot ! for the solution ..I am getting DateTime values as `Tue Dec 22 2015 12:11:38 GMT+0530 (India Standard Time)` ..How can i get it like `2015-Dec-22 12:11:38` format.. – Lara Dec 31 '15 at 05:48
  • Okay i added the function and now trying to add the code in my table as ` ` but getting empty value..Why ? – Lara Dec 31 '15 at 10:30
  • 1
    Write your code as ``, notice the function takes a string, not date object. Also, make sure the function is loaded before this section of html. – Arghya C Dec 31 '15 at 10:35