1

I am trying to download stock data using an api, and for the api to work I need to request the date in the YYYYMMDD format, however I would like it to output YYYY-MM-DD.

Currently I am using this code to request the data:

write(symbol, ";", exchange, ";", bar.date.replace(' ', '; '), ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)

Since the API gives me the output: "YYYYMMDD HH:MM:SS", I have already used the command .replace(' ', '; '), so that it puts a semicolon behind the date output. Now i also would like to format the date output to YYYY-MM-DD, but I think because I am using the .replace i cannot seem to find a way to let this work myself.

Could anyone here help me out?

Jack S.
  • 13
  • 4
  • 1
    You should use Python's Datetime module for this (link: https://docs.python.org/3/library/datetime.html). – felipe May 22 '19 at 19:15
  • You can use this as reference (https://stackoverflow.com/a/16288609/1305461), and this portion of the documentation to help (https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior). – felipe May 22 '19 at 19:17
  • 1
    String only (ugly): `s0 = "YYYYMMDD HH:MM:SS"`, `s1 = "".join([s0[:4], "-", s0[4:6], "-", s0[6:]])`, `print(s1)`. – CristiFati May 22 '19 at 19:18
  • 1
    The safest way is going to be to parse the output into a `datetime`, probably with `datetime.strptime`, and then either `strftime` or one of the convenience methods like `.isoformat()`. – Peter DeGlopper May 22 '19 at 19:23
  • I would follow @PeterDeGlopper advice on this OP. Makes your code more readable, and it's def. more proper. I posted resources for documentation above. – felipe May 22 '19 at 19:28
  • Thanks for the comments! I went with N. Chauhan's answer since it made the most sence to me, but Pete's should work aswell. – Jack S. May 22 '19 at 19:40

5 Answers5

3

I don't know why other answers are suggesting regex when we have the datetime module for this. Use strptime to convert an arbitrary string to a datetime, and then convert back to a string with strftime. The format options are at the bottom of the docs: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior

import datetime as dt

date = '20190522 20:00:24'
new_date = dt.datetime.strptime(date, '%Y%m%d %H:%M:%S').strftime('%Y-%m-%d')
roganjosh
  • 12,594
  • 4
  • 29
  • 46
  • 1
    No offense taken dude. You are anyway one of the my favorite SO users :D (Commenting here because the other post was deleted) – Sheldore May 29 '19 at 19:03
2

No need of regular expressions or string replace. Just use the built in datetime module.

from datetime import datetime

bar.date.strftime(“%Y-%B-%d”)
Ben Richards
  • 3,437
  • 1
  • 14
  • 18
0

You could convert the string into a list of characters. Then insert hyphens at the desired indexes:

date = list(bar.date.replace(' ', ';'))
date.insert(4, '-')
date.insert(7, '-')  # not 6 as the first hyphen shifts them up
write(symbol, ";", exchange, ";",
      ''.join(date),
      ";", bar.open, ";", bar.high,
      ";", bar.low, ";", bar.close,
      ";", bar.volume)

This all assumes that the date format is always guaranteed to be in the same format.

N Chauhan
  • 3,407
  • 2
  • 7
  • 21
  • Awesome man, I have been bugging myself for over an hour to figure this out. it worked great! Since I am a new user I don't think I can up your answer, but thanks a lot! – Jack S. May 22 '19 at 19:27
0

Fast way using regex :

import re
a='20180230;HHMMSS'
re.sub(r'(\d{4})(\d{2})(\d{2}).*',r'\1-\2-\3',a)

Output:

'2018-02-30'

The code finds 4 numbers in a row, then two numbers in a row and then two other numbers in a row and puts a hiphen inbetween those three groups. Then, it ignores anything that comes after those three groups.

Juan C
  • 5,846
  • 2
  • 17
  • 51
0

Try this import datetime from datetime
str(datetime.now()).replace(" ","-")[:10]

Dayananda
  • 305
  • 1
  • 2
  • 11