I am having a project which will have country dropdown at the top to select countries. For example USA, India and Hong Kong. Depends on the country selection several features will be enabled or disabled.
In project I am having base country by default for example India. So whenever any user will come. I want to store its login datetime as per Indian standard time regardless user is from Hong Kong, USA or India. My webserver is hosted in USA.
Is that possible?
What you want to do is store any datetime value as UTC. Which means storing as a time zone independent value.
You can either set some sort of insert trigger in your MS-SQL server (I assume you use MS-SQL because of the asp.net part). There you determine the UTC time like this.
`-- MS SQL (use instead of GETDATE())
DECLARE #currentTime datetime
SET #currentTime = GETUTCDATE()`
If you want to do it in code just use UtcTimeNow to access the UTC time.
// C# (use instead of DateTime.Now)
DateTime currentTime = DateTime.UtcNow;
If you want you can always convert the UTC time to any given local time. By using the TimeZoneInfo class.
That way you have the right time in your database (UTC) and can convert it to the appropriate time for the User.
The most important part when dealing with times and timezones is to store it as UTC. That way it's can be transported and compared easily.
If you save your time in unixtime, you can have a field in the user table called offset. Where it's value is how much the system needs to offset the time.
Then you'd just fetch that variable and save that as a session value or something similar. And use that to offset the datetime in the system.
For example. Hong Kong is +8 hours from GMT. Now in GMT Unixtime is: 1242293400 (9:30 am). 8 hours is 28800 seconds. You would then store this value in your server. So the time would be 1242322200 (5:30 pm same day)
IF you are using SQL Server 2008, look at the new datetime datatypes which allow you to store the offset/timezone as well.
Related
I want current time of India country in nopcommerce 4.5.
I used the following code for getting the current date time, i.e. DateTime.UtcNow in this line of code date is coming perfect, but according Indian time is not showing - it showing some other countries only.
For ex: in India, right now 19-06-2022 05:57 PM is happening but in database it storing 2022-06-19 12:23:44.0000000.
Is there any configuration setting for set country time or anything else?
First, check the time and zone of your server. If the time is correct with the zone, you can convert it to the time of any other country.
There is no method to find out the current time of any country directly in NopCommerce. However, with the DateTimeHelper class, you can convert an UTC time into a user's current zone time or vice versa.
Method 1:
var destinationDateTime = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
var userTime = await _dateTimeHelper.ConvertToUserTimeAsync(DateTime.UtcNow, DateTimeKind.Utc, destinationDateTime);
Method 2:
var userTime = await _dateTimeHelper.ConvertToUserTimeAsync(DateTime.UtcNow, DateTimeKind.Utc);
Note: For method 2, if the store has permission (datetimesettings.allowcustomerstosettimezone setting) to select time zone for the customer
and if the customer selects India as his time zone then he will be
able to see Indian time all the time. And if he does not have
permission to select the time zone, then he can see all the time in
the default time zone of the store.
You can use system.timezoneinfo class to get all the available Standard timezones and convert the current time to that time zone time. In your case it could be
var indiaTimeZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
var indiaTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, indiaTimeZone);
To check all the available time zone you can do
var zones = TimeZoneInfo.GetSystemTimeZones();
foreach (TimeZoneInfo zone in zones)
{
Console.WriteLine(zone.Id);
}
Check this question for more info.
it showing some other countries only.
The system is storing the date in UTC actually. If you compare UTC and Indian time, you'll see the difference matches with your example.
As for your problem, check out the DateTimeHelper class in Nop.Services\Helpers. There are a number of methods there you can use to get the current system time, or to convert time to and from UTC/User time.
The date/time data in the database is stored as Oracle Time Stamp with Time Zone (02-FEB-2013 13:25:00 US/PACIFIC).
When I read the data I need to determine the difference between this time and user's current local time. Using above, I can get user's local time offset:
OracleTimeStampTZ dtzLastActivity = new OracleTimeStampTZ(dr["LAST_ACTIVITY_TZ"].ToString());
TimeSpan tsOffset = dtzLastActivity.GetTimeZoneOffset();
Can I get the local time, maybe using TimeZoneInfo, knowing only the offset, so I can then subtract the time in database from local time to get the difference?
US/Pacific is an IANA/Olson time zone identifier - not a Windows time zone id. So you can't use the TimeZoneInfo classes for it.
Instead, use a TZDB implementation, such as NodaTime. It has full awareness of these types of time zones, and can do the type of calculation you are looking for.
BTW - US/Pacific as actually an alias for America/Los_Angeles. You can find a list of these time zones here.
UPDATE
Sorry, I overlooked that you are using OracleTimeStampTZ. This appears to already implement the TZDB, so you should be able to use it directly. Try the following:
var span = DateTime.UtcNow - dtzLastActivity.ToUniversalTime().Value;
I am try storing my date values in UTC format into my SQL Server DB and then convert them to local time for displaying, seem to work fine, I can see that my DB date values stored are different(I presume it's converted to UTC already), retrieving it and display is also accurate until I try to test a different timezone by changing it in the Date and Time option(right bottom of Windows time settings in the task bar) so to "migrate" myself. Apparently, the dates still remain as I am in my own country even though the timezone which I changed to has a 3 hrs difference.
Can somehow please advice on a way to test the date display on a different timezone?
Thanks.
Storing date values to SQL Server:
DateTime dateFrom = DateTime.Parse(startDateTime).ToUniversalTime();
DateTime dateTo = DateTime.Parse(endDateTime).ToUniversalTime();
parameters.Add(new SqlParameter("#StartDateTime", dateFrom));
parameters.Add(new SqlParameter("#EndDateTime", dateTo));
Retrive from DB and Display:
DateTime date = DateTime.UtcNow.ToLocalTime();
date = DateTime.Parse(dr["StartDateTime"].ToString()).ToLocalTime();
litDateTimeFrom.Text = date.ToString("dd MMM yy hh:mm tt");
date = DateTime.Parse(dr["EndDateTime"].ToString()).ToLocalTime();
litDateTimeTo.Text = date.ToString("dd MMM yy hh:mm tt");
First thing, I am assuming that you have changed time zone information on the server machine (because that is what will be used in ToLocalTime method). If not then that's what you need to do first for testing.
Secondly, have you tried restarting IIS (or your web application) after changing the system time zone? That is necessary because time-zone information could have been cached within .NET framework (in TimeZoneInfo class).
Said all that, to me, this does not make sense. Typically, time-zone information need to flow from user (client) machine because you want to show user local time (as relevant to his time-zone). So it would mean that you need to figure out the current user's time zone based on a) culture info from browser or b) what is stored in his profile data (perhaps country or actual time-zone) c) similar scheme. Once, user time-zone is known, you can use
TimeZoneInfo class to convert UTC time to corresponding local time.
I will be entering some values into a database based on input from users. One value will be the date and time of the input.
I'm not sure how this works at all, but a user from e.g. New York enters some data and the website is hosted in the UK...what time will be stored?
Up until now I had been using the "now()" function to record the date and time, but just occured to me that this could happen! What else should I use?
Thanks,
UTC, works in any time zone, you can either save all dates in your storage as UTC or use proper type that includes time zone data, for example in SQL 2008 you can use datetimeoffset
Hi i am developing a website where user will select the timezone from a dropdown. And in my database the datetime are stored in UTC format. How i can display his profile all other things as per timezone selected by user.
You can store the timezone offset for the user and apply that to the UTC time each time you want show the date/time.