Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert datetime to time

Tags:

c#

.net

I have an object "2/17/2011 6:46:01 PM".I want to convert this object to 6:46 PM

like image 310
Aswathi Avatar asked Feb 17 '11 07:02

Aswathi


People also ask

How do I convert datetime to just time?

DateTime testDateTime = new DateTime(2021,09,21,03,13,22);// Creating TimeOnly object from DateTime.

How can I get only time from datetime in SQL?

How do you get just the time from a DateTime in SQL? SELECT GETDATE();– Output: 2019-03-31 08:12:08.600. SELECT CAST('2019–03–31 08:12:08.600' AS TIME)– Output: 08:12:08.6000000. SELECT CONVERT(VARCHAR(10), CAST('2019–03–31 08:12:08.600' AS TIME), 0)– Output: 8:12AM.

How do I convert datetime to seconds?

To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.


2 Answers

string myDateString = "2/17/2011 6:46:01 PM";
DateTime datetime = DateTime.Parse(myDateString);
string timeString = datetime.ToShortTimeString();

Console.WriteLine(timeString); // 6:46 PM

You can format the parsed datetime to a string in many other ways, but the ToShortTimeString does exactly what you want.

like image 165
Sapph Avatar answered Oct 12 '22 02:10

Sapph


You can format the object as

strdate = convert.todatetime(object);
strdate .tostring("hh:mm tt");

or

strdate.toshorttime();
like image 40
Shyam s Avatar answered Oct 12 '22 01:10

Shyam s