Solved

Handling GMT date issue to enable/disable button

Posted on 2013-05-20
3
374 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

929 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

13 Experts available now in Live!

Get 1:1 Help Now