How to Get Only Time from Date-Time C#

Getting only time of a datetime object

You can use the ToString method with the appropriate formatting string:

var time = date.ToString("H:mm");

how to convert date time to only time in c#

TankDetailsModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(item.timestamp, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString("HH:mm");

this might help you

How to get only the Time from DateTime datatype

You can use DateTime.TimeOfDay

var model = from Ts in db.TimeSpans
where Ts.StartTime.TimeOfDay < startTime
select Ts;

Edit based on comments, you can use EntityFunctions.CreateTime

var model = from Ts in db.TimeSpans 
let time = EntityFunctions.CreateTime(Ts.StartTime.Hours,
Ts.StartTime.Minutes,
Ts.StartTime.Seconds)
where time < startTime
select Ts;

c# show only Time portion of DateTime

Assuming that

DateTime PgTime;

You can:

String timeOnly = PgTime.ToString("t");

Other format options can be viewed on MSDN.

Also, if you'd like to combine it in a larger string, you can do either:

// Instruct String.Format to parse it as time format using `{0:t}`
String.Format("The time is: {0:t}", PgTime);

// pass it an already-formatted string
String.Format("The time is: {0}", PgTime.ToString("t"));

If PgTime is a TimeSpan, you have a few other options:

TimeSpan PgTime;

String formattedTime = PgTime.ToString("c"); // 00:00:00 [TimeSpan.ToString()]
String formattedTime = PgTime.ToString("g"); // 0:00:00
String formattedTime = PgTime.ToString("G"); // 0:00:00:00.0000000

Getting Date or Time only from a DateTime Object

var day = value.Date; // a DateTime that will just be whole days
var time = value.TimeOfDay; // a TimeSpan that is the duration into the day

Getting only hour/minute of datetime

Try this:

var src = DateTime.Now;
var hm = new DateTime(src.Year, src.Month, src.Day, src.Hour, src.Minute, 0);

How to get date only if time is 00:00:00 from DateTime c#

After a while a wrote method for myself.

public static string ConvertToMyDateTimeFormat(Nullable<DateTime> value, CultureInfo IFormateProvider)
{
if (value.HasValue)
{
if (value.Value.TimeOfDay.Ticks > 0)
{
return value.Value.ToString(IFormateProvider);
}
else
{
return value.Value.ToString(IFormateProvider.DateTimeFormat.ShortDatePattern);
}
}
else
{
return string.Empty;
}
}


Related Topics



Leave a reply



Submit