0

I'm trying to translate a date into the equivalent milliseconds since epoch using JIRA smart values.

For example: Get 2020-05-27T02:11:32.000 as 1590509492000

The docs say you can also use formatting from the Java DateTimeFormatter class (link).

I've tried all of the following. Is this possible?

{{now.format("n")}}
{{now.format("A")}}
{{now.toCurrentTime.format("nnnn")}}
{{now.fullDateTime.format("AAAA")}}",
{{now.fullDateTime.format("n")}}",
{{now.fullDateTime.format("A")}}",
{{now.toCurrentTime.millis}}",
{{now.toDateTimeAtCurrentTime.format("n")}}",
{{now.toDateTimeAtCurrentTime.format("A")}}",
{{now.toDateTimeAtCurrentTime.millis}}"
Ivo Mori
  • 2,177
  • 5
  • 24
  • 35
  • 1
    I assume your question isn't about finding a solution in Java but for Atlassian JIRA, right? I edited your question under that assuption. // Otherwise, if you actually were asking for a solution in Java then your question would be a duplicated of [How to convert a date to milliseconds](https://stackoverflow.com/questions/26637168/how-to-convert-a-date-to-milliseconds/26637209). – Ivo Mori May 27 '20 at 04:16
  • The tags should be changed to add Jira, and drop 'java'. – Basil Bourque May 27 '20 at 04:37
  • James Blake, your question has been edited substantially, and I am seriously in doubt whether you intended to ask the question as originally posted or the question as it stands now. Could you please clarify? Because we’d like to help you with whatever is your question. – Ole V.V. May 28 '20 at 17:22
  • The link to DateTimeFormatter is on https://support.atlassian.com/jira-software-cloud/docs/smart-values-date-and-time-functions/ at "Refer to the Java documentation for pattern syntax". It seems the format characters of DateTimeFormatter are what the page is getting at, not specifically its methods. – ProgrammersBlock May 28 '20 at 19:51
  • 1
    Please try {{now.millis}}. According to https://support.atlassian.com/jira-software-cloud/docs/use-smart-values-to-manipulate-and-format-dates/ you should be able to access the attributes of the date. The full list of attributes is given at the end of https://support.atlassian.com/jira-software-cloud/docs/smart-values-date-and-time-functions/ – ProgrammersBlock May 28 '20 at 20:04
  • I've tried what @ProgrammersBlock suggested `{{now.millis}}` But it's returning something that I don't completely understand. Here's and example `now=2021-03-11T20:46:21.5+0000 milis=586000000` Any idea? So far, I haven't been able to get current millis since epoch in Jira. – dabug Mar 11 '21 at 20:48

3 Answers3

0

That’s true, it can be done.

    ZoneId zone = ZoneId.of("Asia/Vladivostok");
    DateTimeFormatter epochMilliFormatter = new DateTimeFormatterBuilder()
            .appendValue(ChronoField.INSTANT_SECONDS)
            .appendValue(ChronoField.MILLI_OF_SECOND, 3)
            .toFormatter();

    LocalDateTime ldt = LocalDateTime.parse("2020-05-27T02:11:32.000");
    ZonedDateTime zdt = ldt.atZone(zone);
    String formattedMilliseconds = zdt.format(epochMilliFormatter);
    System.out.println("Formatted into milliseconds since the epoch: " + formattedMilliseconds);

Output from this example snippet is your desired:

Formatted into milliseconds since the epoch: 1590509492000

I don’t know your time zone, so please substitute Asia/Vladivostok in the code if it didn’t happen to be that one.

How it fits into Jira, I don’t know, though, sorry.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Doubtful the OP was/is looking for a Java solution – and if that'd be the case then it'd be a duplicated question ;) – Ivo Mori May 27 '20 at 06:31
  • @IvoMori I agree that it’s unclear, and the OP will have to tell us. In any case it is *not* a duplicate of [How to convert a date to milliseconds](https://stackoverflow.com/questions/26637168/how-to-convert-a-date-to-milliseconds/26637209) since a `DateTimeFormatter` was explicitly asked for, and none of the answers under that other question gives that. – Ole V.V. May 27 '20 at 06:40
0

If you are still wondering how to do it, this does the trick:

{{now.withYear(1970).withDayOfYear(1).withHour(0).withMinute(0).withSecond(0).withMillis(0).diff(now).millis}}
0

I recently got the solution for this, and it is:

{{#now}}toMillis{{/}}

That's it!

Here's the link to the question I posted in Atlassian community: https://community.atlassian.com/t5/Jira-questions/Get-millis-since-epoch-for-Jira-Automation/qaq-p/1636440#M474154

dabug
  • 123
  • 2
  • 11