简体   繁体   中英

How to convert string to date in pyspark

I have a file name from which I'm extracting date: some_file_name_20201103114823.csv using substring :

substring(input_file_name(),16,8)

I extracted date part, which is now string: 20201103

How can I convert this string to date in format: MM-dd-yyyy ?

This is what i did, formatted string and then cast it to date - not pretty but does the work:

to_date(concat(substring(input_file_name(),16,4),lit("-"),substring(input_file_name(),20,2),lit("-"),substring(input_file_name(),22,2)),"yyyy-MM-dd")

Reference: https://sparkbyexamples.com/pyspark/pyspark-substring-from-a-column/#:~:text=In%20PySpark%2C%20the%20substring(),using%20substring()%20from%20pyspark .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM