Solved

How to use client date rather than server date

Posted on 2011-02-25
6
432 Views
Last Modified: 2012-05-11
Hi,

I have a web application written using vb.net.

There are a number of stored procedures that include date field parameters. These are passed in using the SqlDbType.DateTime datatype via a SQLParameter. This works fine.

My issue is that I use the Now() function in the aspx.vb code behind files to retrieve the current date/time which is then passed to the stored procedure.

This works fine when testing on my development machine but when using the site via the web host (based in USA) I am getting the US date/time stored in the database which is 6 hours different.

Does anyone know a solution so that I can use the client datetime and set the sqlparameter using this via the code behind files.

Thanks in advance,

Dave
0
Comment
Question by:daveswarbrick
6 Comments
 
LVL 14

Expert Comment

by:quizwedge
ID: 34978155
I've got two suggestions that could work.

The first is to to just use Date.Now.AddHours(TimeZoneOffset). Store TimeZoneOffset somewhere you can access it across the site. That way if you ever move hosts, you only have to change the variable once.

The second way is to use getdate() in your SQL. This would have SQL get the date rather than the web host.
0
 
LVL 14

Expert Comment

by:robasta
ID: 34978172
>remember, client's data should not be trusted, (some dates on clients computers will not be accurate: what happens when users' date is in the future, or if it is still in the 90s?)

I suggest you use the server time, but add an offset based on your actual timezone ( DateTimeOffset).

see here: http://msdn.microsoft.com/en-us/library/bb384267.aspx
0
 
LVL 1

Author Comment

by:daveswarbrick
ID: 34978257
Could you please provide a code example to replace;

DateAuthorised = Now()

which will add 6 hours to the time returned above.

Also, is there a way to make the offset changed based on daylight saving, ie, the putting the clock's forward and back?

Thanks,

Dave



0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 14

Accepted Solution

by:
robasta earned 500 total points
ID: 34978290
0
 
LVL 1

Author Closing Comment

by:daveswarbrick
ID: 34978758
Thanks robasta!

Now using a function to return the UTC rather than using Now()
0
 
LVL 8

Expert Comment

by:Anurag Agarwal
ID: 34978836
Dave if u r sure with the time difference that it is  6hrs (may be +6 hrs or -6hrs ) you can simply add or subtract to the server time for eg

   Dim DateAuthorised As DateTime = Now()
        DateAuthorised.AddHours(+6)
        'subtract hrs
        DateAuthorised.AddHours(-6)

        'if some minutes are also
        DateAuthorised.AddMinutes(+30)
        DateAuthorised.AddMinutes(-30)


this may solve the problem but be sure that how you manage the difference of time.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question