Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have 4 columns in database, when i login it shold store username and date and time. but i am getting date with time. i dont want like that i want to store separate date and time
Posted
Comments
DaveAuld 22-Sep-11 4:44am    
Why do you want to store it as 2 seperate fields?
jayanthik 22-Sep-11 5:02am    
i want to track login and logout time at the end of the day i want to calculate hw many hrs he spended so ineed to store time separately
Tejas Vaishnav 22-Sep-11 4:48am    
Please See your table structure so i can give you a query for storing date and time separately...
jayanthik 22-Sep-11 5:02am    
i want to track login and logout time at the end of the day i want to calculate hw many hrs he spended so ineed to store time separately

It's not normally a good idea to store them separately - a DateTime variable is not a date and a time as in "24/11/2008 13:45:16" it is a "this number of milliseconds since an agreed starting point" so specifically storing a time alone is a bit difficult, and would be somewhat wasteful.
Perhaps what you want to do is store a difference in times (a Timespan) instead? That could be the number of milliseconds since midnight, which could then be applied to any base DateTime.
 
Share this answer
 
Comments
jayanthik 22-Sep-11 5:02am    
i want to track login and logout time at the end of the day i want to calculate hw many hrs he spended so ineed to store time separately
OriginalGriff 22-Sep-11 5:07am    
So it is a bad idea to store the time separately - what if he logs in at 23:50, and out at 00:10 the following day? The elapsed time is 20 minutes, but if you store only the time, you don't know that.
C#
string timeonly = DateTime.Now.ToShortTimeString();
string dateonly = DateTime.Now.ToShortDateString();
 
Share this answer
 
SQL
CREATE TABLE UserLogin (
	ID INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
	UserName NVARCHAR(20),
	LoginDate Date,
	LoginTime Time
)


IF YOU INSERT DATA WITH THE USE OF STOREPROCEDURE THEN USE THIS AS YOUR PROCEDURE

SQL
DECLARE @userName NVARCHAR(20);
--SET @userName = 'Tejas Vaishnav';
INSERT INTO UserLogin (UserName,LoginDATE,LoginTIME) VALUES (
@userName,
cast(convert(varchar,getdate(),101) as date),
cast(convert(varchar,getdate(),108) as time)
)


or you want it from your code behind page or c#
then use this

C#
DateTime.Now.ToShortDateString();
DateTime.Now.ToShortTimeString();


For More Detail on DateTime and Convert or Cast see this link
DateTime[^]
 
Share this answer
 
v4
Comments
jayanthik 22-Sep-11 5:32am    
there is no datatype like time and date in sql how can i?
Tejas Vaishnav 23-Sep-11 1:29am    
Please know me which version of Your SQL-SERVER is, my answer is using SQL SERVER 2008
abintj 22-Sep-11 5:32am    
How to do this in SQL Server 2005???
Tejas Vaishnav 23-Sep-11 1:26am    
http://msdn.microsoft.com/en-us/library/ms187928.aspx

See this Artical this will expanin how to work with datetime and conversion
it will help you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900