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
Showing Difference Between Two Datetime Values in Hours
How Can a Word Document Be Created in C#
Why Some Types Do Not Have Literal Modifiers
Is Using a Mutex to Prevent Multiple Instances of the Same Program from Running Safe
How to Force All Referenced Assemblies to Be Loaded into the App Domain
Creating a Comma Separated List from Ilist<String> or Ienumerable<String>
If a Folder Does Not Exist, Create It
.Net Hashtable VS Dictionary - Can the Dictionary Be as Fast
ASP.NET Core 1.0 on Iis Error 502.5
When Should I Use "Using" Blocks in C#
How to Call Base.Base.Method()
How to Get Values of Selected Items in Checkboxlist with Foreach in ASP.NET C#
Method Call Using Ternary Operator
Regex Match Multiple Times in String
How to Fix a Opacity Bug with Drawtobitmap on Webbrowser Control
What Is the Equivalent of the Java Bigdecimal Class in C#
Get the Serial Number of Usb Storage Devices in .Net Core 2.1