c#


Remove Seconds from DateTime c#


I have a section of code that converts the unix timestamp value to a standard time, however it is returning hours, minutes, and seconds. I would like it to only return Hours and Minutes.
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString();
convertSunsetTime = UnixTimeStampToDateTime(currentSunsetTime).ToString();
private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
return dtDateTime.TimeOfDay;
}
and the moment the varibles convertSunriseTime and convertSunsetTime come back with seconds.
So you want to format your value to show hours and minutes. You can do this:
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString("hh\\:mm");
Here, the parameter to the ToString() method specifies that you are only interested in getting the hours and minutes parts into the resulting string. This is called a custom TimeSpan format string. There is also a set of standard ones: Standard TimeSpan format strings
this can do the work:
private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp)
{
DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);
dtDateTime = DateTime.FromMinutes((int)dtdateTime.TotalMinutes);
return dtDateTime.TimeOfDay;
}
Can you try this way
convertSunriseTime = UnixTimeStampToDateTime(currentSunriseTime).ToString();
convertSunsetTime = UnixTimeStampToDateTime(currentSunsetTime).ToString();
<pre>private static TimeSpan UnixTimeStampToDateTime(long unixTimeStamp) </pre>
<pre>{</pre>
<pre>DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);</pre>
<pre>dtDateTime = dtDateTime.AddSeconds(unixTimeStamp);</pre>
<pre>return dtDateTime.TimeOfDay;</pre>
<pre>}</pre>

Related Links

transfer data from one page to another page in C# ASP.NET [closed]
Uuencoded image to byte array
how do i replace exact phrases in c# string.replace
Entity Framework Database First Stored Procedure with different return types
Reference redirects at a solution level
Set equivalent in C# [duplicate]
Notify users using SqlDependency
Using Trim in multiple where clause of linq with Join clause
A route named 'MS_attributerouteWebApi' is already in the route collection
Modifiy JSON Date time object format
Can I specify the element name of the items when I inherit from List<T>
Convert C++ union of structs to C#
Select method not found on JArray
VS 2008: Creating a web service from an existing wsdl file - Getting error
.NET C# - Both Bitmap and Image Loads Images Smaller than Expected
Unity - Null Reference when instantiating new object

Categories

HOME
gcc
algolia
sql-server-2014
pyspark
ssh
android-ndk
bigtable
arcgis
boxplot
app-store
checkpoint
schema
osx-yosemite
genexus
openbsd
osclass
solidity
captcha
react-select
appsettings
wallet
scotty
sonarlint
single-page-application
keystone
polybase
fastboot
wai-aria
oracle10g
workflow-foundation
libxml2
apm
slim-lang
choco
flickr
android-scrollview
perf
dotnetnuke-module
framebuffer
android-graphview
irr
logan-square
infovis
runner
xmldom
flask-socketio
cancan
playframework-2.3
group
qt3d
fileopendialog
konakart
vga
airwatch
hfp
shtml
chomsky-normal-form
biginsights
uinavigationitem
firebase-tools
optimistic-locking
aiff
javacompiler
behance-api
opencobol
mixradio
xtify
myrrix
grape-api
pechkin
kohana-orm
azure-pack
stringstream
http-compression
mmo
urlfetch
linqdatasource
webpage-screenshot
pdf-reader
idtabs
joomla1.7
n2
cinder
http-response-codes
nyromodal
webdb
versions
code-golf
large-data-volumes
file-storage
file-icons

Resources

Encrypt Message