Time Formating

  • Thread starter Thread starter Hector Santos
  • Start date Start date
H

Hector Santos

Whats the skinny on formatting? This is call to show an online timer
and it intermittently will show the milliseconds

DateTime dtTimer; // initial time.

private void start(object sender, EventArgs e)
{
dtTimer = DateTime.Now; // initialize it
}

private void timer1_Tick(object sender, EventArgs e)
{
TimeSpan dt2 = DateTime.Now - dtTimer;

//These two will show the ticks.

lbTimeOnline.Text = dt2.ToString();
lbTimeOnline.Text = string.Format("{0:HH:mm:ss}",dt2);

// this is the best i can get, but its based on local settings
// I need to fix it at hh:mm:ss

lbTimeOnline.Text = string.Format("{0:T}",dt2);

}

Man, I feel like in kindergarden again relearning all sorts of basic
stuff. <g>
 
dt2.ToString("hh:mm:ss");

Ahh, sorry. I though that would work...
Anyway, you have to format individually.

String.Format("{0}:{1}:{2}", dt2.Hours.ToString("00"),
dt2.Minutes.ToString("00"), dt2.Seconds.ToString("00"));
 
Hector said:
Whats the skinny on formatting? This is call to show an online timer
and it intermittently will show the milliseconds

If you just want to show the milliseconds, it's probably easiest to just
get the TotalMilliseconds property of the TimeSpan value, and format
that as a single number.

I've always wondered why the TimeSpan type doesn't provide more
elaborate formatting options. But as "kndg" says, if you want something
more like what's available from DateTime, you have to do it yourself. :(

Pete
 
Peter said:
If you just want to show the milliseconds, it's probably easiest to just
get the TotalMilliseconds property of the TimeSpan value, and format
that as a single number.

I've always wondered why the TimeSpan type doesn't provide more
elaborate formatting options. But as "kndg" says, if you want something
more like what's available from DateTime, you have to do it yourself. :(

Pete


Yeah, that is what I did at first, but it was stupid. :)

Here is how I did it using a split:

TimeSpan dt2 = DateTime.Now - dtTimer;
lbTimeOnline.Text = dt2.ToString().Split('.')[0]; // chop off msecs

The split works like other string splitters in other languages, where
if there is no '.' parts, you still get the first index anyway. IOW,
you always get String[0] of the string array, even if its no '.' parts.

But something is up IFormat interface because an explicit:

String.Format("HH:mm:ss", dt2);

*should* always give the programmer wants he wants and no more. I
hate developing with "oddball unknowns" :)

Thanks
 
Hector said:
[...]
But something is up IFormat interface because an explicit:

String.Format("HH:mm:ss", dt2);

*should* always give the programmer wants he wants and no more. I hate
developing with "oddball unknowns" :)

Just because you named your variable "dt2" as if it represented a
DateTime value, that doesn't mean that you can use its actual TimeSpan
value with formatting intended for DateTime.

As I said, I agree that there should be better formatting support for
TimeSpan. But the "HH:mm:ss" is for DateTime; if you don't pass a
DateTime, it won't give you what you want, nor is there really much
reason to expect it would.

Pete
 
kndg said:
Ahh, sorry. I though that would work...
Anyway, you have to format individually.

String.Format("{0}:{1}:{2}", dt2.Hours.ToString("00"),
dt2.Minutes.ToString("00"), dt2.Seconds.ToString("00"));


Ahhh, so this is how you can pad numbers. I was using

Right("00",val(number),2)

Clumsy!

In C/C++, you would do use this format:

sprintf(szTime,"%02d:%02d%02d", hour, mins, secs);

which left pads the 2 digit numbers.

In .NET, AFAIK, you can set the length, but it doesn't pad.

String.Format("{0,02}:{2,02}:{2,02}", hours, mins, secs);

It puts in a white space instead. :(

You have to wonder if the .NET designers never use other languages
that offers padding in formats? I don't get it. I'm still new to the
C# language, so I have to think that its there hidden the doc.net
land, right? :)
 
Peter said:
Hector said:
[...]
But something is up IFormat interface because an explicit:

String.Format("HH:mm:ss", dt2);

*should* always give the programmer wants he wants and no more. I
hate developing with "oddball unknowns" :)

Just because you named your variable "dt2" as if it represented a
DateTime value, that doesn't mean that you can use its actual TimeSpan
value with formatting intended for DateTime.

As I said, I agree that there should be better formatting support for
TimeSpan. But the "HH:mm:ss" is for DateTime; if you don't pass a
DateTime, it won't give you what you want, nor is there really much
reason to expect it would.

I don't know what that means, nor see the logic regarding expectation:

TimeSpan ts = DateTime.Now - InitialDateTimeNow;

is the time difference, and the docs shows you can use "HH:mm:ss" to
format it. A programmer provided format is *expecting" to do format as
you told it, and no more. It wasn't "HH:mm:ss.ff" which allows for
the msecs, but "HH:mm:ss" The expectation is what you tell it.

Anyway, done deal. I used a split to truncate it to guarantee it is
never part of the string output and display.

Thanks
 
[...]
In .NET, AFAIK, you can set the length, but it doesn't pad.

String.Format("{0,02}:{2,02}:{2,02}", hours, mins, secs);

It puts in a white space instead. :(

IIRC, comma is for aligment and that's why it put the white space.
If you need the padding, use the colon instead. My previous code could
be shorten to,

String.Format("{0:00}:{1:00}:{2:00}", dt2.Hours, dt2.Minutes, dt2.Seconds);

Regards.
 
Anyway, done deal. I used a split to truncate it to guarantee it is
never part of the string output and display.

I would consider using split as a hack and be very very cautious about
it. If you look at the TimeSpan.ToString() method it would return below
format.

[-][d.]hh:mm:ss[.fffffff]

So, your dt2.ToString().Split('.')[0] would give wrong result if the
timespan value exceed 24 hours.

Regards.
 
Peter said:
Hector said:
[...]
But something is up IFormat interface because an explicit:

String.Format("HH:mm:ss", dt2);

*should* always give the programmer wants he wants and no more. I
hate developing with "oddball unknowns" :)

Just because you named your variable "dt2" as if it represented a
DateTime value, that doesn't mean that you can use its actual TimeSpan
value with formatting intended for DateTime.

As I said, I agree that there should be better formatting support for
TimeSpan. But the "HH:mm:ss" is for DateTime; if you don't pass a
DateTime, it won't give you what you want, nor is there really much
reason to expect it would.

Ok, I see the distinction now.

The reason it was 'hungarianly' named dt2 was because of my early
trials and tributations with DateTime and DateTimeOffSet and trying to
use the DateTimeOffSet.ToString("HH:mm:ss") before realization the
different of two DateTime is a TimeSpan. :) Its now:

TimeSpan ts2 = DateTime.Now -dt1;
String.Format("{0:00}:{1:00}:{2:00}",ts2.Hours, ts2.Minutes,
ts2.Seconds);

Thanks fellas.
 
kndg said:
Anyway, done deal. I used a split to truncate it to guarantee it is
never part of the string output and display.

I would consider using split as a hack and be very very cautious about
it. If you look at the TimeSpan.ToString() method it would return below
format.

[-][d.]hh:mm:ss[.fffffff]

So, your dt2.ToString().Split('.')[0] would give wrong result if the
timespan value exceed 24 hours.


Ok, thanks for pointing it out. I don't think it was a hack, just how
the expected formatting was outputted with TimeSpan.ToString() or the
difference of two DateTime as arg[0] in a String.Format. IOW, I
realize now the formatting does not apply with an inherent reference
to TimeSpan variable:

String.Format("{0:HH}:{0:mm}:{0:ss}", DateTime.Now - dt1);
String.Format("{0:HH:mm:ss}", DateTime.Now - dt1);

Anyway, the current solution is:

TimeSpan ts2 = DateTime.Now - dt1;
String.Format("{0:00}:{1:00}:{2:00}",ts2.Hours, ts2.Minutes,
ts2.Seconds);

Thanks:

PS: Don't need the day, wrapping is ok. :)
 
Back
Top