# Datetime operations

**URL:** <https://discourse.julialang.org/t/datetime-operations/122507>\
**Category:** New to Julia\
**Tags:** dataframes, time\
**Created:** [November 11, 2024, 5:22pm UTC](https://discourse.julialang.org/t/datetime-operations/122507 "2024-11-11T17:22:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SergeantMike67](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergeantmike67/32/25103_2.png) [@SergeantMike67](https://discourse.julialang.org/u/SergeantMike67)\
**Post date:** [November 11, 2024, 5:22pm UTC](https://discourse.julialang.org/t/datetime-operations/122507/1 "2024-11-11T17:22:06Z")

</div>

How does one get a datetime column in a Dataframe to be just the time in the format of “HH:MM:SS”. Currently, those with a time entered, reads as 1899-12-30T13:00:00 and I need it to be just 13:00

The reason is that I will add this to the previous column which is the Date (example 2019-04-22T00:00:00) and then subtract off the InitalDate to get the Elapsed Days for each row in the DataFrame

For you math types it’s

ED=(df.PickDay+df.PickTime)-df.StartDate

ED is then stored in the dataframe for further use.

This seems to be quite simple but I appear to be missing something.

---

<div class="post-metadata">

**Author:** ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)\
**Post date:** [November 11, 2024, 6:37pm UTC](https://discourse.julialang.org/t/datetime-operations/122507/2 "2024-11-11T18:37:35Z")

</div>

```julia
Dates.format(now(),"H:M")

```

---

<div class="post-metadata">

**Author:** ![SergeantMike67](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergeantmike67/32/25103_2.png) [@SergeantMike67](https://discourse.julialang.org/u/SergeantMike67)\
**Post date:** [November 12, 2024, 3:18pm UTC](https://discourse.julialang.org/t/datetime-operations/122507/3 "2024-11-12T15:18:46Z")

</div>

Thank you but here is the issue I am running into.

The data I would like to perform the operation on is in a DataFrame and when I try

`Dates.format(df.PickTime,"H:M") `

where df.Picktime is a DateTime datatype.

I get the error  
`MethodError: no method matching format(::typeof(Dates.format), ::String)`  
`The function `format` exists, but no method is defined for this combination of argument types`

I’m sure it is a something easy I am not thinking of but I cannot seem to get it work.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [November 12, 2024, 3:37pm UTC](https://discourse.julialang.org/t/datetime-operations/122507/4 "2024-11-12T15:37:35Z")

</div>

`Dates.format` expects a single DateTime as first argument. If you pass a Vector, you have to apply the function to each element, e.g. via broadcasting:  
`Dates.format.(df.PickTime, "H:M")`  
Note the additional dot.

But this was not your initial problem. To add or subtract from DateTime variables you have to convert them to Periods (see [Dates · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Dates/#Period-Types)), e.g.

```julia
julia> pick_time = DateTime(2024, 4, 22, 13, 9)
2024-04-22T13:09:00

julia> DateTime(1899, 12, 30) + Dates.CompoundPeriod(Hour(pick_time), Minute(pick_time))
1899-12-30T13:09:00

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [November 12, 2024, 5:35pm UTC](https://discourse.julialang.org/t/datetime-operations/122507/5 "2024-11-12T17:35:04Z")

</div>

It seems you’re misusing DateTime, consider using just Dates.Time, or after you’ve figured this out make such a column from your DateTime. I was thinking/hoping it would also save you some space (but it does not, would with some alternative type…):

```julia
julia> dump(Dates.Time(now()))
Time
  instant: Nanosecond
    value: Int64 63159145000000

julia> dump(Dates.DateTime(now()))
DateTime
  instant: Dates.UTInstant{Millisecond}
    periods: Millisecond
      value: Int64 63867115971632

```

Time should be able to fit into UInt16 if you really want to, down to minute, and almost if down to seconds, but not quote, accuracy to every other would fit… log2(24_60_60) = 16.4 bits.

Time in some databases might be only (2 or) 4 bytes, but I see it’s actually also 8 bytes also in PostgreSQL in (down to microsecond, even if you do not take advantage of it), Date is 8 bytes in Julia, unlike in PostgreSQL, where it’s 4 bytes:

> **[8.5. Date/Time Types](https://www.postgresql.org/docs/current/datatype-datetime.html)**
>
> 8.5. Date/Time Types # 8.5.1. Date/Time Input 8.5.2. Date/Time Output 8.5.3. Time Zones 8.5.4. Interval Input 8.5.5. Interval Output PostgreSQL supports …

@SergeantMike67 in MS SQL Server:

> **[Date and Time Data Types and Functions - SQL Server (Transact-SQL)](https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16)**
>
> Links to Date and Time data types and functions articles.

Time is “3 to 5” bytes. Not sure if it applies to your Access database, likely though, Maybe I was wrong and smaller than 8 is possible with PostgreSQL, but it seemed not from my quick reading. You can use Access with any database including it, i.e. as a front-end.

---

<div class="post-metadata">

**Author:** ![SergeantMike67](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergeantmike67/32/25103_2.png) [@SergeantMike67](https://discourse.julialang.org/u/SergeantMike67)\
**Post date:** [November 12, 2024, 5:56pm UTC](https://discourse.julialang.org/t/datetime-operations/122507/6 "2024-11-12T17:56:33Z")

</div>

Interesting. The data is coming in through a query to an Access database. I am not sure how to control the type in the query but I can convert the column to Time.
