Skip to main content
Solved

Best way to remove fractional seconds in Date Time

  • April 4, 2022
  • 6 replies
  • 2064 views

itsmatt
Celebrity
Forum|alt.badge.img+47

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.

Best answer by takashi

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)

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

6 replies

spatialexjames
Contributor
Forum|alt.badge.img+10

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.


redgeographics
Celebrity
Forum|alt.badge.img+62

How about:

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

 


spatialexjames
Contributor
Forum|alt.badge.img+10

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


redgeographics
Celebrity
Forum|alt.badge.img+62

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.


takashi
Celebrity
  • Best Answer
  • April 4, 2022

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)


itsmatt
Celebrity
Forum|alt.badge.img+47
  • Author
  • Celebrity
  • April 4, 2022

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