Solved

How to use client date rather than server date

Posted on 2011-02-25
6
430 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now