Skip to content

Instantly share code, notes, and snippets.

@jossef
Created April 20, 2016 11:43
Show Gist options
  • Select an option

  • Save jossef/f945de471f19ac801838e1683ff3adad to your computer and use it in GitHub Desktop.

Select an option

Save jossef/f945de471f19ac801838e1683ff3adad to your computer and use it in GitHub Desktop.
python utility function to convert javascript / java date format string -> python format string
def convert_to_string(dt, date_format):
date_format = date_format.replace("yyyy", "%Y")
date_format = date_format.replace("yy", "%y")
date_format = date_format.replace("MMMM", "%B")
date_format = date_format.replace("MMM", "%b")
date_format = date_format.replace("MM", "%m")
date_format = date_format.replace("M", "%m")
date_format = date_format.replace("dddd", "%A")
date_format = date_format.replace("ddd", "%a")
date_format = date_format.replace("dd", "%d")
date_format = date_format.replace("HH", "%H")
date_format = date_format.replace("mm", "%M")
date_format = date_format.replace("ss", "%S")
return dt.strftime(date_format)
@jossef
Copy link
Author

jossef commented Apr 20, 2016

>>> now = datetime.datetime.now()
>>> convert_to_string(now, 'dd/MM/yyyy HH:mm:ss')
'20/04/2016 14:41:17'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment