Solved

Best way to remove fractional seconds in Date Time

  • 4 April 2022
  • 6 replies
  • 430 views

Userlevel 4
Badge +26

I'm trying to format a human readable date time and I want to include the seconds but nano seconds are a bit much.

 

As far as I can tell there is no date time flag which is just seconds without the fractional seconds so it seems like you need to remove them with a non-datetime tool. Am I missing something?

 

Date times were created using the DateTimeNow function.

icon

Best answer by takashi 4 April 2022, 12:43

View original

6 replies

Badge +2

It is strange that the documentation refers to "%S" as "Two digit seconds" in the DateTimeConverter but includes all the fractional seconds too. One way could be to stick a StringSearcher with regular expression "^[0-9]+" before the DateTimeConverter and it'll drop the decimals.

Userlevel 5
Badge +25

How about:

@int(@DateTimeFormat(@Value(datetime),%Y%m%d%H%M%S))

 

Badge +2

How about:

@int(@DateTimeFormat(@Value(datetime),%Y%m%d%H%M%S))

 

You'd have to be careful about the rare case of 59.9 seconds rounding up to an invalid 60 (Edit: not an issue with "int") and any 0 being dropped at the start if it's just a timestamp

Userlevel 5
Badge +25

You'd have to be careful about the rare case of 59.9 seconds rounding up to an invalid 60 (Edit: not an issue with "int") and any 0 being dropped at the start if it's just a timestamp

The int function truncates rather than rounds.

Userlevel 2
Badge +17

Hi @virtualcitymatt​ , alternatively, you can also use SubstringExtractor or @Substring function.

@Substring(@DateTimeNow(),0,14)

 

If you would like to round the datetime value, this expression would be possible.

@Substring(@DateTimeAdd(@DateTimeNow(),PT0.5S),0,14)

Userlevel 4
Badge +26

Hi @virtualcitymatt​ , alternatively, you can also use SubstringExtractor or @Substring function.

@Substring(@DateTimeNow(),0,14)

 

If you would like to round the datetime value, this expression would be possible.

@Substring(@DateTimeAdd(@DateTimeNow(),PT0.5S),0,14)

Thanks - yeah the substring approach was my work around but it seemed like I should have been able to Format it to drop the decimals. Ahh well - I've created an idea for it. It's not the first time I've had to do this.

 

https://community.safe.com/s/bridea/a0r4Q00000I0oS8QAJ/add-an-option-in-datetimeconverter-datetimeformat-to-specify-second-precision

Reply