Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to calculate date only and time only from datetime object.
When I insert int table it saved in table "2/1/2012 12:00:00 AM" by this format.
I want date as 2/1/2012 and time as 12:21:12 PM OR AM.
Posted
Updated 23-May-12 22:52pm
v2
Comments
Prasad_Kulkarni 24-May-12 4:54am    
Not clear. For me. Please elaborate some more
Technoses 24-May-12 5:04am    
mark as solution if problem got solved and dont forget to rate it...
Prosan 1-Jun-12 7:15am    
what do you want is not clear do you want save data in table with date and time or save date in other field and time in other field. not clear elobrate it.

you should use formating for this

like

C#
DateTime dt;
dt = Convert.ToDateTime(ds.Tables[0].Rows[0][0]); // date from database


for date

C#
dt.ToString("dd/MM/yyyy");


for time

C#
dt.ToString("hh:mm:ss ttt");
 
Share this answer
 
Try this:
C#
DateTime dt = DateTime.ParseExact("29-04-2012", "dd-MM-yyyy", CultureInfo.InvariantCulture);


This will help you more:
C#
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
 
String.Format("{0:y yy yyy yyyy}", dt);  // "8 08 008 2008"   year
String.Format("{0:M MM MMM MMMM}", dt);  // "3 03 Mar March"  month
String.Format("{0:d dd ddd dddd}", dt);  // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}",     dt);  // "4 04 16 16"      hour 12/24
String.Format("{0:m mm}",          dt);  // "5 05"            minute
String.Format("{0:s ss}",          dt);  // "7 07"            second
String.Format("{0:f ff fff ffff}", dt);  // "1 12 123 1230"   sec.fraction
String.Format("{0:F FF FFF FFFF}", dt);  // "1 12 123 123"    without zeroes
String.Format("{0:t tt}",          dt);  // "P PM"            A.M. or P.M.
String.Format("{0:z zz zzz}",      dt);  // "-6 -06 -06:00"   time zone



Have a look on this article:
Easy String to DateTime, DateTime to String and Formatting[^]
Convert date from Hijri Calendar to Gregorian Calendar and vise versa[^]
Formatting a DateTime for display - format string description[^]
 
Share this answer
 
Comments
hitech_s 24-May-12 6:20am    
my 5!
Prasad_Kulkarni 19-Jul-12 4:51am    
Good one +5
C#
DateTime myDT = DateTime.Now.Date;


That will give you just the date!

EDIT: Also remember, if your field in SQL is a DateTime field, it will always save a time, even if you dont supply one. If you want just the date, change the column to be a Date column.

When you get the data back in C#, if you want to just get the string of the date, just use:

C#
DateTime myDT = DateTime.Now;
myDT.ToShortDateString();
 
Share this answer
 
v3
simply run this query in sql server...


SQL
select rtrim(convert(char,getdate(),103)) + '  '+ ltrim(rtrim(substring(convert(char,getdate(),109),13,8))) + ' '+ ltrim(substring(convert(char,getdate(),109),25,2)) as time



mark it solution if you got your answer and dont forget to rate it...


Thanks

Ashish
 
Share this answer
 
v2
Comments
Member 7909353 24-May-12 5:40am    
In place of getdate() i have to pass datetime object from c#
AshishChaudha 24-May-12 5:45am    
it a SQL query, you can directly run for your table..

in place of getdate() put your datetime field name.

select rtrim(convert(char,yourfieldname,103)) + ' '+ ltrim(rtrim(substring(convert(char,yourfieldname,109),13,8))) + ' '+ ltrim(substring(convert(char,yourfieldname,109),25,2)) as time from tablename
Member 7909353 24-May-12 8:32am    
Will it return datetime object?
Member 7909353 28-May-12 6:29am    
When I insert into table , value is inserted with time.I do not want time.

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