Solved

How to use client date rather than server date

Posted on 2011-02-25
6
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

696 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