Setting project localization date - asp.net

How can I setup my ASP.net MVC 4 project to use my country datetime, because I am hosting my app on the cloud - apphabour?
I am now using Datetime.Now() to retrieve the latest app?

Datetime.now returns the current time and time zone defined by the settings for the server clock or the domain time service for the ad controller. Normally dates stored in a database ( or code ) are returned as UFC date time so that web clients /windows clients can convert utc time to local time defined by the settings on each workstation.

Related

Dynamics CRM 365 on premise unified interface webresource reading date field from form getting date in UTC

We have an on premise tenant and using unified interface.
I have a form which has a date field and its behaviour is set as User Local.
Date Field Setting On the same form I have a web resource that displays the same date field data on form load. I am using parent.Xrm.Page.getAttribute("fieldname").getValue() to get the date field value on web resource.
I know the date is getting stored in UTC format. It displays fine on the form but on the web resource it is one day behind.
Is it the timing issue that form has got the date in UTC format but before it converts it to local format, web resource reads it.
Please advise.
I have found the issue. If the time zone in CRM personal setting is different than the time zone setting of the machine then this problem happens. In my case CRM personal setting had Sydney as the time zone but machine was set for Brisbane time zone. –I

Stored procedure works on Local host but not on production server

I am currently working on an ASP.net based fleet management application. The application fetches the daily exchange rate and allows users to make transactions based on the exchange rate. There transactions are of two kinds
1. Local Currency (exchange rate =1 )
2. USD
I have written a simple select query read exchange rate value from a table in database. This query works when I access it using local host but when I try to access it using the deployed application it doesn't work
The SQL query is
SELECT id_exchange_rate, Exchange_rate
FROM NVP_ExchangeRate
WHERE CAST(Exchange_Rate_date as date)=CAST(#date_today as date)
AND Manual_rate IS NULL
It is enclosed inside the following block
if (currency_actual == "USD")
{
}
Either of the two conditions is failing.
Can someone help me with this ?
Also, The same application is deployed in 2 QA servers. It is working in both the QA servers.
Just thought that I'd add an answer to my question.
In the first query, I am getting the value of current date from the application (DateTime.Now in c#) and then passing this date to my SQL query. My Application server was based in India. However, my database server was based in Mexico.
So when I use DateTime.Now on an Indian server, it returns the current Indian date. When I take this date and go to my database query which is in Mexico (which is almost 12 hours behind India), the dates don't match as Mexico is almost a day behind us.
The root cause of this issue could be attributed to poor programming on my part.

Which date and time will insert into SQL Server Database if I wrote in sql query as Getdate() for createDate field?

If I published my Asp.Net solution on a server and server date time is correct.
Now, When any user access that URL from LAN network on another PC and that PC date time is not correct.
In that case if I write
Insert Into tblComp(1,'XYZ','Jam',GETDATE()) in my query.Then which date time will be inserted.Server or PC(which have accessing that URL)
tblComp structure
********************
id int,
SName varchar(50),
SAdd varchar(50),
CreateDate Datetime
*********************
The query executes on the database server, the database server will provide its current date and time.
Please note that this date and time will be in whatever time zone the database server is in as well.
From the documentation of GETDATE():
Returns the current database system timestamp as a datetime value without the database time zone offset. This value is derived from the operating system of the computer on which the instance of SQL Server is running.
If, on the other hand, you actually want the local date and time of the client, you will have to provide that through a parameter. On an ASP.NET site, this is not all that easy because the "client code" will the ASP.NET application, running on the web server, so instead of the date and time of the database server you would effectively use the date and time of the web server.
To actually get the client date and time (that is, the local date and time of the computer running the web browser that is browsing the site), you would probably have to resort to javascript, though I'm completely unfamiliar with how you would do that.

How to expire a particular row in SQL Server once a date is reached?

It may be very simple scenario for experienced developers, but as a beginner I have been hanging around this for a long time. I am creating a website in asp.net where a user can purchase a packages. These packages have some expiry date associated with them (e.g. 2 months after purchase, 6 months, etc.).
The problem is how to expire each package exactly at the same date and time that i have calculated on the basis of purchase date. How to do this thing?
I am using SQL Server express edition 2008 and asp.net 4.0 .
Rather than deleting the records, you could put a WHERE clause in place which would exclude the records based on their expiry date:
SELECT PackageName
FROM Package
WHERE ExpiryDate>GETDATE()
However if you still wish to remove them from the database, you could put a SQL Job in place which is ran every day/hour etc which would delete records which have expired:
DELETE FROM Package
WHERE ExpiryDate<GETDATE()
Depends a bit on what you mean by expire - do you want to delete the data, flag the record, or just hide the data from the user's screen.
Either way, you need to add the expiry date as a column to your table (?packages), and save the calculated expiry date into this column.
You then have options such as :
Run a job to delete / expire the record once it has expired, i.e. CURRENT_TIMESTAMP is > ExpiryDate (SQL Express doesn't have SQL Agent, so you would need to e.g. write a windows service, or hook into Windows Task Scheduler to do this).
Or, change your application to 'check' the value of the ExpiryDate (e.g. DateTime.Now >= ExpiryDate), and then lock the user out / hide the package from the UI.

TimeZone.CurrentTimeZone in an ASP.NET app

How does TimeZone.CurrentTimeZone work if run in an ASP.NET app? Does it return the timezone of the application pool running the application? If using impersonation, does it return the timezone of the user its impersonating?
It is the time zone on the computer where the code is executing. This is stored in HKLM i.e. it is a machine level setting not a user level. So it doesn't matter if impersonaltion is used. The value would still be the same. Check TimeZone.CurrentTimeZone which is TimeZoneInfo.Local

Resources