Solved

Handling GMT date issue to enable/disable button

Posted on 2013-05-20
3
392 Views
Last Modified: 2013-06-02
In my application I want to enable/disable attendance button. I am recording marked attendance dates in GMT. Few users who are come in mid-shift are finding this attendance button enabled in alternate days. For example if any user has marked attendance today, he might not be able to mark attendance next day because button is disabled as it is still finding the same GMT date when last attendance was marked.

I am using below code to match user's last marked attendance with current GMT date.

Declare @Minutes numeric(10,2)
Declare @CurrentDate DateTime
SET @Minutes = DATEDIFF(minute, GetUTCDate(), GetDate())
SET @CurrentDate = Convert(Varchar(20), DateAdd(minute, @Minutes, GetUTCDate()),111)

Open in new window


With this code, the @CurrentDate is always obtained with 12:00 AM time. I feel there is some mistake in this code.

I also want to know whether the above code is required at all because I just want to match the last recorded GMT date with the current GMT date.

Please note that GetDate alone will not work because this is a ASP.NET application and our database server is in US whereas clients are in other nation.

I am using SQL Server 2005 database. Please suggest solutions for both SQL Server 2005 and 2008.
0
Comment
Question by:rpkhare
  • 2
3 Comments
 
LVL 29

Assisted Solution

by:QPR
QPR earned 250 total points
ID: 39182537
Your convert to varchar 111 is truncating your date to yyyy-mm-dd which is why you have no minutes
0
 
LVL 29

Expert Comment

by:QPR
ID: 39182548
Is this what you need?

Declare @Minutes numeric(10,2)
Declare @CurrentDate DateTime
SET @Minutes = DATEDIFF(minute, GetUTCDate(), GetDate())
SET @CurrentDate = Convert(Varchar(20), DateAdd(minute, @Minutes, GetUTCDate()))
select @currentdate
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 39183067
not sure why you are using convert(varchar...) at all here
conversion to varchar isn't required when setting a datetime by DATEADD
Returns a new datetime value based on adding an interval to the specified date.
try this:

Declare @Minutes numeric(10,2)
Declare @CurrentDate DateTime
SET @Minutes = DATEDIFF(minute, GetUTCDate(), GetDate())
SET @CurrentDate = DateAdd(minute, @Minutes, GetUTCDate())

select @currentdate, getdate(), GetUTCDate(), @Minutes


also note datediff isn't going to return 2 decimals either
DATEDIFF produces an error if the result is out of range for integer values.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

831 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