Display negative number as HH:MM (1 Viewer)

RyLane

Registered User.
Local time
Yesterday, 20:34
Joined
Feb 4, 2014
Messages
60
Hi,

I'm converting minutes to display as HH:MM but have an issue when the number is negative. Anyone know how to write this so I don't end up with a negative in front of the minutes?

Format([Variance]\60,"00") & ":" & Format(([Variance] Mod 60),"00")

i.e. -193 gives me -03:-13, whereas I would like -03:13

Thanks,

Ryan
 

MarkK

bit cruncher
Local time
Yesterday, 17:34
Joined
Mar 17, 2004
Messages
8,187
Code:
Format((Abs([Variance]) Mod 60),"00")

There's also the TimeSerial() function that is worth being aware of . . .
Code:
? Format(TimeSerial(0, 193, 0), "hh:nn")
. . . but this doesn't propagate your minus sign for negative time.
 

RyLane

Registered User.
Local time
Yesterday, 20:34
Joined
Feb 4, 2014
Messages
60
Thought we had but that turns values like 5:09 into 5:9

Still looking
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 19:34
Joined
Feb 28, 2001
Messages
27,319
1. Convert your Variance from integer seconds to a time field.

Code:
Dim VTime as Double
Dim ATime as Date

later in the code

Code:
VTime = Float( [Variance], DOUBLE )  'trust me...
ATime = CDate( VTime / 86400.0 )     'which will probably get converted to 86400#
If Atime < 0 then ATime = -ATime      'if negative, be positive about it.

When you need it, do a Format$( ATime, "hhh:mm" ) to get the time string. You MIGHT need "hhh:0m" - it always confuses me about forcing leading zeros in a time format. However, it's an easy enough experiment..

Note that you still have Variance available in its original form so could make an explicit sign in the output string by testing that variable.
 
Last edited:

RyLane

Registered User.
Local time
Yesterday, 20:34
Joined
Feb 4, 2014
Messages
60
Mark,

9 is the absolute of 09

Doc,

I can't get that to work at all, doesn't like "Double" I get an expression error as soon as I put it in
 

plog

Banishment Pending
Local time
Yesterday, 19:34
Joined
May 11, 2011
Messages
11,669
9 is the absolute of 09

Yes. And when you format 9 with the method you have chosen, you get 09. So what's the issue?
 

RyLane

Registered User.
Local time
Yesterday, 20:34
Joined
Feb 4, 2014
Messages
60
Was putting the Abs() in the wrong spot. Does what it's supposed to now.

But I also want to be able to sort this value from high to low, which I guess is a whole other thing. Sorting this way just treats all values as positive.
 

plog

Banishment Pending
Local time
Yesterday, 19:34
Joined
May 11, 2011
Messages
11,669
Won't sorting the field [Variance] accomplish what you want?
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 01:34
Joined
Sep 12, 2006
Messages
15,710
maybe something like this

Code:
display =  Format(abs([Variance])\60,"00") & ":" & Format((abs([Variance]) Mod 60),"00"
 
  if variance<0 then
      display = "-" & display
 end
 

vbaInet

AWF VIP
Local time
Today, 01:34
Joined
Jan 22, 2010
Messages
26,374
It's minutes to seconds, divided by 86400, then format:
Code:
Format(([Variance]*60)/86400, "hh:nn")
 

RyLane

Registered User.
Local time
Yesterday, 20:34
Joined
Feb 4, 2014
Messages
60
Won't sorting the field [Variance] accomplish what you want?

When I sort it doesn't distinguish between positive and negative values. So -07:00 will appear before 08:00.
 

plog

Banishment Pending
Local time
Yesterday, 19:34
Joined
May 11, 2011
Messages
11,669
What datatype is Variance? You already mentioned in your first post it contains negative values. I don't understand how it can recognize them when you are using your format function, but doesn't recognize them when you want to sort.
 

RyLane

Registered User.
Local time
Yesterday, 20:34
Joined
Feb 4, 2014
Messages
60
What datatype is Variance? You already mentioned in your first post it contains negative values. I don't understand how it can recognize them when you are using your format function, but doesn't recognize them when you want to sort.

Variance is Decimal. It sorts properly without applying the format function but doesn't after. I don't get it either
 

MarkK

bit cruncher
Local time
Yesterday, 17:34
Joined
Mar 17, 2004
Messages
8,187
In a string, 9 sorts as larger than 10, because "9" is larger than "1". In a string the comparison starts at the left, not at the decimal point. And this . . .
Code:
Format(abs([Variance])\60,"00") & ":" & Format((abs([Variance]) Mod 60),"00")
. . . is a string.
 

plog

Banishment Pending
Local time
Yesterday, 19:34
Joined
May 11, 2011
Messages
11,669
So my advice a few posts back stands:

Won't sorting the field [Variance] accomplish what you want?
 

RyLane

Registered User.
Local time
Yesterday, 20:34
Joined
Feb 4, 2014
Messages
60
Yep, I just wanted to see if there was another way of doing it without bringing Variance into the mix.
 

vbaInet

AWF VIP
Local time
Today, 01:34
Joined
Jan 22, 2010
Messages
26,374
Post #12 to include a coercion to a date type:
Code:
CVDate(Format(([Variance]*60)/86400, "hh:nn"))
You can apply a format on the above and it will sort properly. However, you should do it as plog advised. A sort can be applied on the Variance field without showing the field so I don't see why you don't want to sort on Variance. Besides, sorting on the Variance field will yield faster results than sorting on the calculated formatted field.
 

Users who are viewing this thread

Top Bottom