I have an object "2/17/2011 6:46:01 PM".I want to convert this object to 6:46 PM
DateTime testDateTime = new DateTime(2021,09,21,03,13,22);// Creating TimeOnly object from DateTime.
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.
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.
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.
You can format the object as
strdate = convert.todatetime(object);
strdate .tostring("hh:mm tt");
or
strdate.toshorttime();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With