我该如何重新格式化日期

时间:2017-06-17 08:44:08

标签: python django chart.js

嘿伙计们,我从oanda收到我的数据并通过chart.js中的django绘制它

class ChartData(APIView):
    def get(request, *args, **kwargs ):


 data = oanda.get_history(instrument='EUR_USD',  # our instrument
                             start='2016-10-01' ,  # start data
                             end=time.strftime("%Y-%m-%d"),  # end date
                             granularity='D')  # minute bars  # 7
    df = pd.DataFrame(data['candles']) # 8

    data50 = oanda.get_history(instrument='EUR_USD',  # our instrument
                             start='2016-10-01',  # start data
                             end='2017-01-01',  # end date
                             granularity='D')  # minute bars  # 7
    df50 = pd.DataFrame(data50['candles']) # 8

    data200 = oanda.get_history(instrument='EUR_USD',  # our instrument
                             start='2016-6-01',  # start data
                             end='2017-01-01',  # end date
                             granularity='D')  # minute bars  # 7
    df200 = pd.DataFrame(data200['candles']) # 8

    history = df['openBid']
    date_labels = df['time']


    history = df['openBid']
    date_labels = df['time']

    data = {
             "history": history,
             "date_labels": date_labels,
        }

    return Response(data)

现在日期的格式如您在屏幕截图中看到的那样。 如何使用以下格式DD / MM / JJJJ格式化日期?

提前致谢

Screenshot

1 个答案:

答案 0 :(得分:2)

在将响应数据传递到模板之前,您可以格式化数据框,因为oanda将返回格式为time的{​​{1}},您可以格式化{{1}与fomat 2014-02-12T14:50:25Z

df['time']

格式化后,DD/MM/YY将变为df['time']=pd.to_datetime(df['time']).dt.strftime('%d/%m/%Y')