Timex.Timezone.convert/2 question

Hey I am using this function, and the result does only show until the seconds, doesn’t show milliseconds, not even the .000000. Why is that?
Before it did show it.
It used to return this:


and now it returns this:


I am using timex 3.4

Can you give a more complete example about how you create the dates and how you convert them?

1 Like

sure, I have the dates saved in the db, and before returning them to the fronted, I run them through the function in the title

def parse_datetime_with_timezone(datetime, timezone), do: Timezone.convert(datetime, timezone)

The dates in the db do not have milliseconds, but it has been like that forever, and as I said before it returned them with the milliseconds.

or how could I add back the milliseconds?

I fixed it liek this:

Timex.add(datetime, Duration.from_microseconds(1))

That would change the time, making comparisons fail and so forth.

Since Timex doesn’t store time in the format shown in the first post, I’m guessing that a display function is being used that elides the trailing zero’s. If you could show that call that you use to print it then can show you how to adjust the formatter to keep the trailing zeros. :slight_smile:

1 Like

Hey, thank you, I’d appreciate that!

So if you can see my second command I had that function to add the time zone to the date, and that function is being called by the view with the date that is contained by the corresponding item.

So what I would need is that the convert returns the date with the trailing zeros.

I used IO.inspect to print the dates out of you meant that

Oh that is why! Timex prints out a shortened representation of the time it represents for debugging purposes. In general inspect is only for debugging purposes, not for an user interactions, instead you will want to use the formatting functions and guide available at:

I wrap up my calls in functions to call, as my workplace likes a very…non-standard way of displaying dates for compatibility with older systems. ^.^;

1 Like

Hey thanks, but how will one of these give me the format I want?

The format specifiers are described here:


I think {ss} is the one you need for displaying the milliseconds.

1 Like

Thanks, but how do I actually use that?
Because this:
DateTime.Formatter.format(datetime, {ss})
obviously says that “ss” does not exist
or this one:


with this one:

Timex.Format.DateTime.Formatters.Default.format(datetime, "{ss}")

it gives back this: {:ok, ""}

You have read the page @OvermindDL1 linked you to? It tells you to use Timex.format.

1 Like

also on another note, I can just deduct that microsecond that I added and then the date value is the same, and it keeps the format

If you want to format a time for presentation use Timex.format, the inspection output is for debugging only and might change its presentation without any warnings.


Hmm, as per the documentation I linked I don’t see why this wouldn’t work for you based on the information given?

iex(1)> Timex.format(DateTime.from_unix!(1549385336000000, :microsecond), "%FT%T.%f%z", :strftime)
{:ok, "2019-02-05T16:48:56.000000+0000"}
iex(2)> Timex.format(DateTime.from_unix!(1549385336000001, :microsecond), "%FT%T.%f%z", :strftime)
{:ok, "2019-02-05T16:48:56.000001+0000"}

actually it gives back an incorrect value,

{:ok, "1970-01-01T00:25:20.672400+0000"}

only the microsecond changes, so I have a normal DateTime at the start and as I saw you use from_unix so I changed the date with the DateTime.to_unix() and then gave it to this function but it only gives 1970-01-01 as a result no matter the date I give to it

ok so at the end I used Timex.set/2 and setting the microsecond field

How is that an incorrect value? It works down to microseconds in formatting, although your DateTime you gave it didn’t seem to have that (not a formatting problem) so it was zero’s.

That’s still changing the displayed time from what it actually is. Rather are you absolutely sure beyond any doubt that your DateTime that you are passing in actually has microsecond time? I’m not sure that changing the displayed time from the stored time is really a good “Solution” post as it could easily break things that other people may try to do the same with if all they read is that post and nothing else. ^.^;

oh nono, maybe I miss typed or something, but the issue was this: my saved times doesn’t have microseconds, but the app expects them for parsing reasons, not for any other use, so I just needed that DateTime to have microseconds in the format.

Then please use proper formatting instead of relying on debug output that may change without a warning at any point in time.