python Time T Remove Operation with ms Millisecond Time Format

  • 2021-10-25 07:24:15
  • OfStack

I won't talk too much, let's just look at the code ~


import datetime
#  Time format  .%f  Milliseconds 
## "%Y-%m-%dT%H:%M:%S.%f"
#  Format   Seconds 
## "%Y-%m-%dT%H:%M:%S"
#  Format   Minutes 
## "%Y-%m-%dT%H:%M"
#  Will   Time in string format, converted to time millisecond format 
update_time = datetime.datetime.strptime(i["UpdatedAt"][:26], "%Y-%m-%dT%H:%M:%S.%f")
#  Convert the time format to   String 
update_time = update_time.strftime('%Y-%m-%d %H:%M:%S.%f')

Supplement: Python T-N Date Missing Issue

I won't talk too much, let's just look at the code ~


#! /usr/bin/env python
# -*- coding:utf-8 -*-
from datetime import datetime, date, timedelta
 
m = [('JD','2018-11-08'),('Aussie FS','2018-10-10'),('Braun FS','2018-11-09'),('Global FS','2018-11-07')]
t = [('JD',	'2018-09-10'),('JD',	'2018-09-11'),('JD',	'2018-09-12'),
('JD',	'2018-09-13'),
('JD',	'2018-09-14'),
('JD',	'2018-09-15'),
('JD',	'2018-11-08'),
('Aussie FS',	'2018-10-02'),
('Aussie FS',	'2018-10-03'),
('Aussie FS',	'2018-10-04'),
('Aussie FS',	'2018-10-05'),
('Aussie FS',	'2018-10-06'),
('Aussie FS',	'2018-10-07'),
('Aussie FS',	'2018-10-08'),
('Braun FS',	'2018-10-28'),
('Braun FS',	'2018-11-08'),
('Braun FS',	'2018-10-30'),
('Braun FS',	'2018-10-31'),
('Braun FS',	'2018-11-01'),
('Global FS',	'2018-09-18'),
('Global FS',	'2018-09-19'),
('Global FS',	'2018-09-20'),
('Global FS','2018-11-08'),
('Global FS',	'2018-09-22'),
('Global FS',	'2018-09-23')
 
]
mm = '2018-11-08'
for x in range(0, len(m)):
    n = []
    n_new = []
    i = 0
    while i < len(t):
        if t[i][0] == m[x][0]:
            n.append([t[i][0], t[i][1]])
        i += 1
    n_new = sorted(list(n))
    if n_new[-1][1] != mm:
        print '%s Maximum business hours are :%s, Have not arrived T-1:%s'%(n_new[-1][0],n_new[-1][1],mm)
 
def get_nday_list(n):
    before_n_days = []
    for i in range(1, n + 1)[::-1]:
        before_n_days.append(str(date.today() - timedelta(days=i)))
    return before_n_days
 
a = get_nday_list(10)
b = get_nday_list(30)
ttt = []
for x in range(0, len(m)):
    d = []
    d_new = []
    i = 0
    while i < len(t):
        if t[i][0] == m[x][0]:
            d.append([t[i][0], t[i][1]])
        i += 1
    d_new = sorted(list(d))
    print d_new
    ll = []
    for xx in range(0,len(d_new)):
        ll.append(d_new[xx][1])
    kk = 0
    while kk<len(a):
        if a[kk] not in ll:
            ttt.append([m[x][0],a[kk]])
        kk +=1
print ' The missing business dates are: %s'%(ttt)
 

Added: django drf json Formatting Date Time Band T Solution Based on python


# models.py 
update_time = models.DateTimeField(verbose_name=u' Update time ', default=timezone.now)

Problem: T in the middle of days and hours


"gmt_created": "2019-05-19T22:47:46.853262"

Solution:


# serializers.py 
class DeviceSerialiser(ModelSerializer):
    #  Formatting Date Formatting 
    update_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S') 
    class Meta:
        model = WaterDevice
        fields = ('__all__')

Related articles: