How to get Client Local Date from TimeZone in asp.net? - asp.net

I have my database server located in U.S. The user makes entry from INDIA the date must be stored of client and not the server. but, unfortunately the server date is stored in database. I am showing the entries to user only of the current date that are made on the day only. Problem is when user enters record till 12:30 PM(Client Machine) are being displayed to user but after 12:30PM (Client Machine), the entries are not being displayed only the entries made after 12:30 PM(Client Machine) are displayed.Any Idea?
Eg: Entry done at 06/05/2013(DD/MM/YYYY) till 12:30 PM are shown when client machine date is 06/05/2013 11:57 AM
but when client machine date is 06/05/2013 12:57 PM the entries enterd are not displayed but entries made after 12:30 PM are displayed. In DB for above eg. created date is stored as 05/05/2013 don't know why?
In .cs file:
public static DateTime GetIndianDate(DateTime dateTime)
{
return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime, TimeZoneInfo.Local.Id,"India Standard Time").Date;
}
To show todays entries only:
shipments = BLL.GetShipmentsByUserIdNDate(userId, Classes.Common.GetIndianDate(DateTime.Now.Date));
public static List<Shipment> GetShipmentsByUserIdNDate(Guid userId, DateTime dateTime)
{
Entities db = new Entities();
List<Shipment> shipments = (from s in db.Shipments
where s.UserId == userId && s.CreatedDate == dateTime
select s).ToList();
return shipments;
}
While Inserting:
shipment.CreatedDate = Classes.Common.GetIndianDate(DateTime.Now.Date);
public static DateTime GetIndianDate(DateTime dateTime)
{
return TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dateTime, TimeZoneInfo.Local.Id,"India Standard Time").Date;
}
So, How will i store the date of client machine? now it still stores servers date only.
Help Appreciated!

Hey For same I Recommend to Store UTC Date time of every Record when You are saving the records.
When you are fetching the records please get the timezone and manipulate the UTC date
Select getutcdate() --Get UTC Date
Select getdate()--Get Indain Standared Date
Select dateadd(minute,30,dateadd(hour,5,getutcdate())) --Get Indian offset +05:30
If You want to Update your date from C# Code please refer this one.But in that case also required UTC date
string utcDateString = System.DateTime.UtcNow.ToString();
DateTime localDate = DateTime.Parse(utcDateString,
CultureInfo.CurrentCulture,
DateTimeStyles.AssumeUniversal);
Hop it Helps you.

Related

Was there a change in timezone for ToLocalTime

Currently I'm using ToLocalTime to convert the UTC date received from my backend. Is there a way to setup my ASP.NET Core 3.0 web site to use a specific timezone when calling ToLocalTime (instead of depending on the host region) -- or should I implement my own method/extension to converting to the time I need?
Official documentation of DateTime.ToLocalTime states:
Note that the exact output depends on the current culture and the local time zone of the system on which it is run.
You can use the TimeZoneInfo static method ConvertTimeFromUtc to convert an instance of DateTime (Note: if it's Kind property is Local you'll get an exception!) from Utc to whatever local time you want (Another note: the Kind property of the result is either Utc or Unspecified - depending on the target TimeZoneInfo).
Code example (copied from documentation page):
DateTime timeUtc = DateTime.UtcNow;
try
{
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
Console.WriteLine("The date and time are {0} {1}.",
cstTime,
cstZone.IsDaylightSavingTime(cstTime) ?
cstZone.DaylightName : cstZone.StandardName);
}
catch (TimeZoneNotFoundException)
{
Console.WriteLine("The registry does not define the Central Standard Time zone.");
}
catch (InvalidTimeZoneException)
{
Console.WriteLine("Registry data on the Central Standard Time zone has been corrupted.");
}

Google Datastore -> Compare DateTime-Property with Date

I just can't find an answer to the following problem:
I have entities in my Google App Engine Datastore which all have a property of the type "Date and time". My Datastore console shows me the format of that property. It looks like this:
2/16/16, 7:20 PM CET
Now I want to do a query that only gives back the entities AFTER a specific date. But how do I format that date? I tried the following:
#ApiMethod(name = "getUpdatedEpisodes", path = "getUpdatedEpisodes")
public List<Episode> getUpdatedEpisodes(#Named("lastUpdate") String lastUpdate) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy, hh:mm a z");
Date date = null;
try{
date = sdf.parse(lastUpdate);
}catch (Exception ex){
}
Query.Filter filter =
new Query.FilterPredicate("lastUpdate",
Query.FilterOperator.GREATER_THAN,
date);
Query q = new Query("Episode").setFilter(filter);
PreparedQuery pq = datastore.prepare(q);
Afterwards I try to set a new date and run this method again. I do the following:
repository.getInstance(context).lastUpdate = DateTime.now(DateTimeZone.forID("CET")).toString("dd/MM/yy, hh:mm a z");
While debugging I can see that this String looks like
2/16/16, 7:24 nachm. GMT+01:00
And if I try my API with this string again, I will still get all my episodes, so no filtering was done. Is this because my Date and the Google Date can't be compared if they aren't the exact same format? How can I fix this?
Rather than using a String for your date property, you can use a Date object:
import java.util.Date;
...
repository.getInstance(context).lastUpdate = new Date();
The Datastore will store this property so that it is sorted in chronological (or anti-chronological) order and query filters will work as expected.
https://cloud.google.com/appengine/docs/java/datastore/entities#Java_Properties_and_value_types
I solved this issue by storing my date in the datastore as millis instead of date and time. This way I only have an 64-bit integer that is easy to compare.

JSON date to asp.net DateTime is changed at runtime

I've searched around and I'm really not sure why this happens.
Most of the time my app runs in GMT from devices using GMT, but I just span a server up in Singapore, so the time is 8hrs ahead. I'm seeing some strange behaviour with DateTime objects parsed from JSON:
My app received a JSON (ISO 8601) date like this:
LastSync=2013-01-10T11:05:38.822Z
I'm using a simple .asmx web-service, that uses the built in JSON serializer for .Net 3.5, the automatically parsed DateTime object returns a date 8hrs ahead of what the JSON says it should be. Here's the function:
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function SyncFlatTable(ByVal tableName As String, ByVal LastSync As DateTime)
Return WebServiceJSON.SyncFlatTable(tableName, LastSync)
End Function
As you can see the LastSync as DateTime argument's value is 8hrs ahead:
The weird thing is if I return Now(), the JSON output from the web service is:
newLastSend=/Date(1357817197087)/
Which using a timestamp to date online converter is no-longer 8hrs ahead.
Is this IIS's fault? I can't see any other culture settings everything else is neutral, why would it change the date? How do I stop it, or do I have to take what I'm given and adjust the date to an invariant culture date?
I got this problem as well, I ended up using the sting type to pass in/out the date info.
It seems that calling .toJSON on any date object, when it's converted to a string representation it is done so using a local date. I needed to adjust this to UTC so that when it's converted it does so with what the date should be for the server:
function removeDateUTCOffSetForServer(obj) {
if (obj) {
for (prop in obj) {
if (obj[prop] instanceof Date) {
var d = obj[prop];
var utc = new Date(d.getTime() - (d.getTimezoneOffset() * 60000));
obj[prop] = utc;
}
}
}
return obj;
}

ASP.NET match users local date and time with the system time

I have some trouble understanding how to match users current date and time with the server time.
Example: let's assume I have a website where users can register themselves. One profile option is that they can select their local time zone. For this there is a drop-down menu from where they can choose the correct time zone. So a user from China will select probably (UTC+08:00) Beijing, Chongqing, Hong Kong, Urumqi, another user from Los Angeles will select (UTC-07:00) Mountain Time (US & Canada) (i assume) and a guy from Paris will select (UTC+01:00) Brussels, Copenhagen, Madrid, Paris.
The web application is running on a server in USA with it's specific time zone ...
Now ... All these users will want to receive an email notification next Friday at 19:00 their local time zone!!!
Here I am lost ... definitely the next Friday at 19:00 is not in the same time for all these users ...
How can I map their profile time zone, so the service running on my site will send the email notification next Friday at 19:00 user's local time zone???
I am at this stage at the moment ... populating the drop down menu with all time zones so users can select their current time zone in their profile.
When the page load's than the dropdown is populated with the time zone:
protected void Page_Load(object sender, EventArgs e)
{
ddlUserTimeZone.DataSource = GetTimeZones();
ddlUserTimeZone.DataTextField = "Name";
ddlUserTimeZone.DataValueField = "ID";
ddlUserTimeZone.DataBind();
}
public Collection<TimeZoneItem> GetTimeZones()
{
Collection<TimeZoneItem> timeZones = new Collection<TimeZoneItem>();
foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
{
timeZones.Add(new TimeZoneItem
{
TimeZoneName = timeZoneInfo.DisplayName,
TimeZoneID = timeZoneInfo.Id
});
}
return timeZones;
}
public struct TimeZoneItem
{
public string TimeZoneName { get; set; }
public string TimeZoneID { get; set; }
}
Now, can you guys help with the matching of the profile time zone with the current time so the email is sent in the correct time?
Thanks in advance!
Are you just setting up this service? If so, run your web servers and database servers on Universal Time Coordinated (UTC or Zulu) time, not a local time zone. Everything is far easier to manage if you do that. I learned this the hard way.
UTC used to be called Greenwich Mean Time; it is timezone +00:00. It doesn't change for daylight savings time like US and European local time does.
This time zone thing is a pain, and worth getting right. Some countries have half-hour time zones.
At any rate, once you know each user's preferred time zone, and the time she wants her notification, you can then convert the time from local to UTC and store it.
Try something like this to get the user's hour and minute into UTC. (Time zone conversions need a date, because they depend on daylight savings time rules. There's one more complication. On the day when a time zone switches from daylight to standard, or vice versa, the UTC time for a notification will change. Most people handle this by recomputing the UTC date and time of the next notification (tomorrow's notification) as they send each notification. Consider this code.
TimeZoneInfo userLocal = ;//user's time zone
int hour = ;//whatever the user wants
int minute = ;//whatever the user wants
DateTime tomorrow = DateTime.Now.AddDays(1);
int year = tomorrow.Year;
int month = tomorrow.Month;
int day = tomorrow.Day;
DateTime notificationTime = new DateTime(year,month,day,
hour,minute,0,
DateTimeKind.Unspecified);
DateTime tomorrowNotificationTime = TimeZoneInfo.ConvertTimeToUtc(
notificationTime,userLocal);
This should get you the UTC time you need to deliver this user's notification tomorrow, in the correct time zone for tomorrow's date.
DateTime should ideally be stored on the server in UTC format.
You have the following data on your server
Timezone info of the user.
The time at which the user needs the notification.
The current local time on your server.
// Convert current local server time to UTC.
var serverUtc = DateTime.UtcNow;
// Convert UTC time to users local time. This gives you the date and time as per the user.
var userTimeZone = TimeZoneInfo.GetSystemTimeZones()[0]; // just an example. Replace with actual value.
var userCurrentTime = TimeZoneInfo.ConvertTime(serverUtc, userTimeZone);
/*
add a day to userCurrentTime till its Friday. Add/subtract minutes till its 7:00PM.
*/
var expectedNotificationTimeUtc = TimeZoneInfo.ConvertTimeToUtc(userCurrentTime, userTimeZone);
/*
1. store the expectedNotificationTimeUtc as the time you want to send the email.
2. your service can check for users their expectedNotificationTimeUtc and
if the UtcNow is within an acceptable range of the that time, send the email.
*/

asp.net timestamp issue

This i my code for storing record information from asp.net 2.0 webform
scmd.Connection = scon; //Connection string
SqlParameter p = scmd.CreateParameter();
recodName = txtrecordname.Text; //form field
todaysdate = DateTime.Parse(txtFrom.Text);
DateTime now = DateTime.UtcNow;
AddParameters("#record", recodName); //adding parameter to stored procedure
AddParameters("#date", todaysdate);
AddParameters("#timeinfo", now);
scmd.CommandText = "sp_InsertRecord";
scmd.CommandType = CommandType.StoredProcedure;
scon.Open();
int i=scmd.ExecuteNonQuery();
if (i > 0)
{
result.Text = "Record Inserted RecordName : " + recodName; //Label displaying recordinfo
dateinfo.Text = "Record inserted on (TimeStamp Info) : " + now; //label displaying time info when user inserted record
}
GridView1.DataBind();
This web application is hosted on server whose timezone is (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi,
Now user from another system access the application whose time zone is (UTC+01:00) West Central Africa,
As you can see i have inserted 'datetime now as Utcnow' but when user view the record inserted it must be in its local datetimeformat
i.e
dateinfo.Text = "TimeStamp Info : " + now; //This label should display local time info currently it displays server local time where the application is hosted
Thanxs for any help
When loading the DateTime from the Database (you said you store UTC there), you need to specifiy the Kind of this time explicitely:
DateTime dateTimeFromDatabase = LoadDateTimeValueFromDatabase();
DateTime utcDate = DateTime.SpecifyKind(dateTimeFromDatabase, DateTimeKind.Utc);
Then, as long as you have the Culture of the web site visitor set correctly, you can simply use .ToLocalTime() from the DateTime to display the Utc value as local time:
DateTime localTimeToDisplay = utcDate.ToLocalTime();
You then use the localTimeToDisplay variable for databinding and you're all set.
Update: To display the current time you can simply do:
dateinfo.Text = "TimeStamp Info : " + DateTime.UtcNow.ToLocalTime();
As UtcNow already sets the Kind of DateTime to UTC.
You need to set the correct culture for the client, though. So you would need to do that either manually or set this in the web.config:
<system.web>
<globalization culture="auto" />
<system.web>
Use javascript to get the current time in the client system and put that in a javascript variable.
How to do it can be found here: http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm
Then assign the javascript variable value to a hidden field variable and access it in the server side.
How to access javascript variable in code behind http://codeasp.net/blogs/joydeep157/microsoft-net/81/accessing-javascript-variable-from-code-behind-and-accessing-code-behind-variable-from-javascript
Now you have the client time in your server variable.

Resources