Click here to Skip to main content
Sign Up to vote bad
good
See more: SQL2005C#ASP.NET
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 23 May '12 - 22:50
Edited 23 May '12 - 22:52

Comments
Prasad_Kulkarni - 24 May '12 - 4:54
Not clear. For me. Please elaborate some more
Technoses - 24 May '12 - 5:04
mark as solution if problem got solved and dont forget to rate it...
Prosan - 1 Jun '12 - 7:15
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.

5 solutions

you should use formating for this
 
like
 
 
DateTime dt;
dt = Convert.ToDateTime(ds.Tables[0].Rows[0][0]); // date from database
        
 
for date
 
dt.ToString("dd/MM/yyyy");
 
for time
 
dt.ToString("hh:mm:ss ttt");
  Permalink  
Try this:
DateTime dt = DateTime.ParseExact("29-04-2012", "dd-MM-yyyy", CultureInfo.InvariantCulture);
 
This will help you more:
// 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[^]
  Permalink  
Comments
hitech_s - 24 May '12 - 6:20
my 5!
Prasad_Kulkarni - 19 Jul '12 - 4:51
Good one +5
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:
 
DateTime myDT = DateTime.Now;
myDT.ToShortDateString();
  Permalink  
simply run this query in sql server...
 

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
  Permalink  
Comments
Member 7909353 - 24 May '12 - 5:40
In place of getdate() i have to pass datetime object from c#
AshishChaudha - 24 May '12 - 5:45
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:32
Will it return datetime object?
Member 7909353 - 28 May '12 - 6:29
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Arun Vasu 339
1 OriginalGriff 320
2 Sergey Alexandrovich Kryukov 315
3 Tadit Dash 221
4 CPallini 178
0 Sergey Alexandrovich Kryukov 9,955
1 OriginalGriff 7,589
2 CPallini 4,028
3 Rohan Leuva 3,422
4 Maciej Los 2,949


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 24 May 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid