473,545 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Format a TimeSpan Without the Decimal Part of the Seconds

If I display a TimeSpan I get something like

00:05:17.621789 1

when what I would like to see is

00:05:18

Is there an easy way to get this output? Try as I might I just can't find
it.

TIA

Charles
Nov 21 '05 #1
10 38602
Here's my thought....

dim TS as String = <GetTimeSpan>.T oString
TS=TS.Substring (0,LastIndexOf( ":")+2)

"Charles Law" <bl***@nowhere. com> wrote in message
news:ef******** ******@TK2MSFTN GP15.phx.gbl...
If I display a TimeSpan I get something like

00:05:17.621789 1

when what I would like to see is

00:05:18

Is there an easy way to get this output? Try as I might I just can't find
it.

TIA

Charles

Nov 21 '05 #2
Charles,
What I normally do when I need custom formatting of a TimeSpan is "convert"
it to a DateTime, then use custom DateTime formatting. Something like:

Dim ts As TimeSpan
Dim dt As DateTime = DateTime.MinVal ue.Add(ts)
Dim s As String

s = ts.ToString() ' default TimeSpan formatting
s = dt.ToString("H: mm:ss") ' custom DateTime formatting
For details on custom datetime formats see:

http://msdn.microsoft.com/library/de...matstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/de...ttingtypes.asp

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Charles Law" <bl***@nowhere. com> wrote in message
news:ef******** ******@TK2MSFTN GP15.phx.gbl...
| If I display a TimeSpan I get something like
|
| 00:05:17.621789 1
|
| when what I would like to see is
|
| 00:05:18
|
| Is there an easy way to get this output? Try as I might I just can't find
| it.
|
| TIA
|
| Charles
|
|
Nov 21 '05 #3
Charles,

In addition to the others,

I concatinate the individual parts with the ":" between them.

Hours:Minutes:S econds

If I am lazy it is

dim str as string = ts.ToString.len gth(8)

(not so nice as documentation)

I hope this helps,

Cor
Nov 21 '05 #4
Hi Jay

Thanks for the reply. As I feared it is not a one-liner. With all the other
formatting options, is this an omission, in your view, that TimeSpan does
not have ToString that can take a format?

Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
message news:e7******** ******@TK2MSFTN GP14.phx.gbl...
Charles,
What I normally do when I need custom formatting of a TimeSpan is
"convert"
it to a DateTime, then use custom DateTime formatting. Something like:

Dim ts As TimeSpan
Dim dt As DateTime = DateTime.MinVal ue.Add(ts)
Dim s As String

s = ts.ToString() ' default TimeSpan formatting
s = dt.ToString("H: mm:ss") ' custom DateTime formatting
For details on custom datetime formats see:

http://msdn.microsoft.com/library/de...matstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/de...ttingtypes.asp

--
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Charles Law" <bl***@nowhere. com> wrote in message
news:ef******** ******@TK2MSFTN GP15.phx.gbl...
| If I display a TimeSpan I get something like
|
| 00:05:17.621789 1
|
| when what I would like to see is
|
| 00:05:18
|
| Is there an easy way to get this output? Try as I might I just can't
find
| it.
|
| TIA
|
| Charles
|
|

Nov 21 '05 #5
Hi Terry

Thanks. I can see that this would work, but I think it confirms that there
is no particularly elegant solution in the way that other formatting works.

Charles
"Terry Olsen" <to******@hotma il.com> wrote in message
news:er******** ******@tk2msftn gp13.phx.gbl...
Here's my thought....

dim TS as String = <GetTimeSpan>.T oString
TS=TS.Substring (0,LastIndexOf( ":")+2)

"Charles Law" <bl***@nowhere. com> wrote in message
news:ef******** ******@TK2MSFTN GP15.phx.gbl...
If I display a TimeSpan I get something like

00:05:17.621789 1

when what I would like to see is

00:05:18

Is there an easy way to get this output? Try as I might I just can't find
it.

TIA

Charles


Nov 21 '05 #6
Hi Cor

This is what I was resigned to doing in the end. I suppose I should write a
helper function so that I can call it from anywhere, or use Jay's suggestion
to add more formatting options.

Incidentally,
dim str as string = ts.ToString.len gth(8)
gives me a syntax error. Is that what you meant or am I taking it too
literally?

Charles
"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:O$******** ******@TK2MSFTN GP14.phx.gbl... Charles,

In addition to the others,

I concatinate the individual parts with the ":" between them.

Hours:Minutes:S econds

If I am lazy it is

dim str as string = ts.ToString.len gth(8)

(not so nice as documentation)

I hope this helps,

Cor

Nov 21 '05 #7
Charles,
dim str as string = ts.ToString.len gth(8)


I typed this in the message, really a stupid error, that VB helps me with
this is the main reason that I like VBNet above C#.

ts.ToString.Sub string(0,8)

as well not checked however probably better.

You knew this........... ....

:-))

Cor
Nov 21 '05 #8
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> schrieb:
What I normally do when I need custom formatting of a TimeSpan is
"convert"
it to a DateTime, then use custom DateTime formatting. Something like:


That's what I do too, but I am still wondering why 'TimeSpan' doesn't have
an overloaded parameterized version of 'ToString' which takes the format
string, not even in .NET 2.0.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #9
Charles,
IMHO Unfortunately it seems that TimeSpan is the "abandoned child" of the
Framework (a.k.a an omission or oversight of the Framework designers). In
that:

- TimeSpan does not implement IConvertible, nor does IConvertible support
TimeSpan (does not have a ToTimeSpan). Along with IConvertible there should
be a TypeCode.TimeSp an
- TimeSpan does not implement IFormattable & have custom formatting similar
to the "Time" part of DateTime.
If TimeSpan was IConvertible friendly then you could use Convert.ToStrin g &
Convert.ToTimeS pan to convert Timespans to & from other types.

If TimeSpan was IFormattable friendly then it would support custom
formatting.

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Charles Law" <bl***@nowhere. com> wrote in message
news:ej******** ******@TK2MSFTN GP10.phx.gbl...
| Hi Jay
|
| Thanks for the reply. As I feared it is not a one-liner. With all the
other
| formatting options, is this an omission, in your view, that TimeSpan does
| not have ToString that can take a format?
|
| Charles
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************ @tsbradley.net> wrote in
| message news:e7******** ******@TK2MSFTN GP14.phx.gbl...
| > Charles,
| > What I normally do when I need custom formatting of a TimeSpan is
| > "convert"
| > it to a DateTime, then use custom DateTime formatting. Something like:
| >
| > Dim ts As TimeSpan
| > Dim dt As DateTime = DateTime.MinVal ue.Add(ts)
| > Dim s As String
| >
| > s = ts.ToString() ' default TimeSpan formatting
| > s = dt.ToString("H: mm:ss") ' custom DateTime formatting
| >
| >
| > For details on custom datetime formats see:
| >
| >
http://msdn.microsoft.com/library/de...matstrings.asp
| >
| > For information on formatting in .NET in general see:
| >
http://msdn.microsoft.com/library/de...ttingtypes.asp
| >
| > --
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Charles Law" <bl***@nowhere. com> wrote in message
| > news:ef******** ******@TK2MSFTN GP15.phx.gbl...
| > | If I display a TimeSpan I get something like
| > |
| > | 00:05:17.621789 1
| > |
| > | when what I would like to see is
| > |
| > | 00:05:18
| > |
| > | Is there an easy way to get this output? Try as I might I just can't
| > find
| > | it.
| > |
| > | TIA
| > |
| > | Charles
| > |
| > |
| >
| >
|
|
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1818
by: Doug | last post by:
Is it possible there is a bug with the System.TimeSpan object in .NET? I am using it to track times and am seeing an unusual situation. From what I understand, there are 1000 milliseconds in a second. I have a test I ran using the TimeSpan object which when I use TimeSpan.Milliseconds I got 658 but if I use TimeSpan.Seconds I get 5. If...
15
14257
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" DateTime.Now.AddMinutes(90) returns "1:30" or "01:30"
4
3533
by: Jaroslav Jakes | last post by:
Hi, crazy, can't find a simple way. Have a table with columns: ElementValue, ElementFormat (both string-format) Would like to format ElementValue according to ElementFormat e.g.
14
4069
by: Michael Barrido | last post by:
I have this for example: Dim iSeconds as int32 = 3600 '3600 seconds is one hour How do i convert it to "01:00:00" ? Please help. Thanks in advance!
5
9241
by: gh | last post by:
I have 2 columns in a grid where the user enters a begining time(TI) and at the end of the day an end time(TO). I then want to take the TO - TI to get the difference and display it as hours and minutes. If the result is 7 hours and 45 minutes I would like it to be able to display it 2 ways, as 7.45 or 7.75, if it was 8 hours then display...
3
62698
by: RicercatoreSbadato | last post by:
I'd like to print a TimeSpan variable in this way: 00:00:00 --hh:mm:ss (whitout the milliseconds) how can I do it?
7
14914
by: WorldIsEnding | last post by:
Hey, Im having trouble with converting a Numeric value (such as a single digit number like 1 or 5) into a time format. I need to convert a number entered into a variable into Hours on the first form, then from Hours into Seconds when a second form is loaded. Anyone got any ideas? If you need more info, let me know and ill post a code...
1
9530
by: =?Utf-8?B?RGFwcGVyRGFuSEBub3NwYW0ubm9zcGFt?= | last post by:
Given the example below, can someone explain why TimeSpan.TotalDays gives a different result than subtracting 2 DateTime.ToOADates? I am completely stumped. Thanks in advance, Dan Example Output: start: 1/1/2007 12:00:00 AM end : 1/1/2007 1:00:00 AM
7
4533
by: pamela fluente | last post by:
My numericUpDowns show numbers in the format 1.500,56 (italy). Instead, I need *invariantly* that they show and accept the format 1,500.56, as in the USA. What's the right way to do that? I have tried to set the current culture to the US culture, but nothing changed. For instance I tried without success (vb) :
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.