Giter Club home page Giter Club logo

Comments (2)

tonythomas01 avatar tonythomas01 commented on August 10, 2024 1

Oh sure. I think my task description was lacking the context. When I said django printing out, I meant Django serialising the DateTime fields. So, we use django-rest framework.

Apparently, you can control it over a settings, like https://www.django-rest-framework.org/api-guide/fields/#datetimefield and this should help us, I guess.

The strings are for the clients on the response, for eg: the createdAt in http://docs.quarantinehelp.space/#/Explore%20and%20assign%20requests%20as%20a%20HL%20participant/get_api_v1_crises__crisisId__affected_participants__participantId__requests_

from quarantine-help-api.

montao avatar montao commented on August 10, 2024

Django prints out weird stuff to the frontend, like "2020-04-04T11:17:37.784674Z", Lets push it to ISO format "2019-09-27T11:01:41.928158+00:00",

If you need humanized/localized output then you might want to use babel and pytz. I used to do something like the following

from datetime import datetime
from pytz import timezone
from babel.dates import format_time
import pytz

FORMAT = '%H:%M / %d-%m-%Y'
MONTHS = ('Jan.', 'Feb.', 'Mar.', 'April.', 'May.', 'June.',
          'July.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.')

# dummy function if you want to use i18 later and the real _ translation
def _(dummy):
    return dummy

def format_datetime_human(to_format, locale='en', timezoneinfo='Asia/Calcutta'):
    import datetime as DT
    import pytz
    utc = pytz.utc
    to_format = DT.datetime(int(to_format.year), int(to_format.month), int(to_format.day), int(to_format.hour), int(to_format.minute))
    utc_date = utc.localize(to_format)
    tzone = pytz.timezone(timezoneinfo)
    tzone_date = utc_date.astimezone(tzone)
    month = MONTHS[int(tzone_date.month) - 1]
    time_str = format_time(tzone_date, 'H:mm')
    date_str = '{0} {1}'.format(tzone_date.day, _(month))
    return "{0} {1}".format(date_str, time_str)

Then I can get output any way I like e.g.

>>> import timepy
>>> import datetime
>>> timepy.format_datetime_human(datetime.datetime.now())
'5 April. 0:04'
>>>

timepy-py.txt

Or a template filter e.g. https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#naturaltime

from quarantine-help-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.