pendulum icon indicating copy to clipboard operation
pendulum copied to clipboard

Initialize from existing `datetime.date` object?

Open jacebrowning opened this issue 6 years ago • 15 comments

pendulum.instance(dt) works for datetime.datetime, but how can I initialize a Pendulum object from an existing datetime.date (specifically a Django DateField)?


My current workaround:

pendulum.parse(str(date_object))

jacebrowning avatar Jul 05 '18 15:07 jacebrowning

from datetime import datetime as dt
import pendulum
pendulum.instance(dt.fromordinal(date_object.toordinal()))

This should work.

Edit:
This is better:

pendulum.datetime(date.year,date.month,date.day)

ghost avatar Jul 21 '18 15:07 ghost

Could we add that behavior to pendulum.instance()?

jacebrowning avatar Jul 21 '18 15:07 jacebrowning

+1

kakulukia avatar Aug 31 '18 12:08 kakulukia

+1

I would also check for datetime.time so we have all possible cases of a datetime covered with Pendulum.

trancee avatar Sep 04 '18 04:09 trancee

+1 This would be a great addition to the API!

joaodlf avatar Dec 12 '18 12:12 joaodlf

+1

SemMulder avatar May 06 '19 13:05 SemMulder

+1

joaomcarlos avatar Sep 27 '19 14:09 joaomcarlos

+1

racerthunder avatar Mar 17 '20 03:03 racerthunder

+1

lonsun avatar Jun 26 '20 02:06 lonsun

+1

Alsanis avatar Jul 06 '20 19:07 Alsanis

+1

rodrigondec avatar Sep 16 '20 19:09 rodrigondec

I would like to pump this. I find doing this over and over and would love if pendulum had this feature built in.

madisvain avatar Apr 27 '21 12:04 madisvain

This is a no-brainer for me. I work with an ORM that returns datetime.date instances, and having to pick them apart to pass them into pendulum.instance feels like the symptom of a missing feature.

AaronBeaudoin avatar Nov 16 '21 16:11 AaronBeaudoin

+1

richieadler avatar Jan 28 '22 02:01 richieadler

I have created a first draft at https://github.com/kakulukia/pendulum .. im missing some tests before opening a puss request, but like to get some feedback first.

kakulukia avatar Mar 01 '22 19:03 kakulukia

+1

blondelg avatar Dec 30 '22 13:12 blondelg

+1

yardensachs avatar May 03 '23 18:05 yardensachs

+1

lena avatar May 03 '23 18:05 lena

+1

mcguigon0033 avatar May 03 '23 18:05 mcguigon0033

+1

jackb2113 avatar May 03 '23 18:05 jackb2113

+1 !

codytowstik avatar May 08 '23 16:05 codytowstik

I've also looked into the documentation & the code in search for this behavior. In the code, I see that even the shorthand class functions to initialize a Date like today first construct a native datetime.date object before they create the instance using its result. This surely would also profit from a Date.instance class function constructor.

https://github.com/sdispater/pendulum/blob/c4e518c950bcf0a4214b36c5992ae27d3c40e47f/pendulum/date.py#L740-L755

FichteFoll avatar Jul 03 '23 23:07 FichteFoll