-3

How to convert below date to unix format using python ?

I have checked for date month and year format but it is not working

Date: "10 Nov 2017"

Please help.

michael
  • 21
  • 8
  • 4
    Duplicate of https://stackoverflow.com/questions/9637838/convert-string-date-to-timestamp-in-python – madprops Nov 14 '17 at 06:22
  • 1
    https://docs.python.org/2/library/time.html – madprops Nov 14 '17 at 06:25
  • madprops@ 01/12/2011 this is different format than "10 Nov 2017" when i used code on this format it gives me an error: ValueError: time data '10 Nov 2017' does not match format '%d/%m/%Y' – michael Nov 14 '17 at 06:26
  • Yeah you need a different format. Check the docs I linked to suit it to your date format, check the table. – madprops Nov 14 '17 at 06:27
  • 1
    Possible duplicate of [How to print date in a regular format in Python?](https://stackoverflow.com/questions/311627/how-to-print-date-in-a-regular-format-in-python) – Jithin Pavithran Nov 14 '17 at 06:30

2 Answers2

0

the problem can be solved as follows

import time
import datetime
s = "01/dec/2011"
time.mktime(datetime.datetime.strptime(s, "%d/%b/%Y").timetuple())
output:
1322697600.0
varnit
  • 1,859
  • 10
  • 21
  • varnit@ in my case date is "10 Nov 2017", if it would have been like "01/12/2011" then the code worked – michael Nov 14 '17 at 06:30
  • i have edited the question you just have to use %b to use months name hope it helps – varnit Nov 14 '17 at 06:32
  • for complete documentation of strptime documentation you can refer to this link https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior – varnit Nov 14 '17 at 06:34
  • If that solved your problem mark it as solved and upvote it thanks in advance – varnit Nov 14 '17 at 06:45
0

As per the doc

You can use %b for Locale’s abbreviated month name.

Try this:

import time
import datetime
s = "10 Nov 2017"
time.mktime(datetime.datetime.strptime(s, "%d %b %Y").timetuple())
Unknown
  • 2,037
  • 3
  • 30
  • 47
  • Thanks.. what if i have to convert "01:53:52" to unix ? – michael Nov 14 '17 at 07:19
  • s = "10 Nov 2017 01:53:52" print(time.mktime(datetime.datetime.strptime(s, "%d %b %Y %H:%M:%S").timetuple())) – Unknown Nov 14 '17 at 07:29
  • but it is not working if I only needed "01:53:52" but not "10 Nov 2017" – michael Nov 14 '17 at 08:43
  • I have used s = "01:53:52" print(time.mktime(datetime.datetime.strptime(s, "%H:%M:%S").timetuple())) but it gives me: OverflowError: mktime argument out of range – michael Nov 14 '17 at 08:44
  • This is different question. But you can refer https://stackoverflow.com/questions/6402812/how-to-convert-an-hmmss-time-string-to-seconds-in-python – Unknown Nov 14 '17 at 08:55
  • How to convert "01/30/2003 02:00 AM" to unix ? Please help – michael Nov 21 '17 at 08:13
  • Refer the doc https://docs.python.org/2/library/time.html. Read it first fully and then come back if you have any issue – Unknown Nov 21 '17 at 08:16