Solved

Handling GMT date issue to enable/disable button

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
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…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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

16 Experts available now in Live!

Get 1:1 Help Now