'The file_date function creates a new file in the current working directory,

import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  ___
  timestamp = ___
  # Convert the timestamp into a readable format, then into a string
  ___
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{___}".format(___))

print(file_date("newfile.txt")) 

Should be today's date in the format of yyyy-mm-dd



Solution 1:[1]

You can use fromtimestamp function to deal with timestamp and strftime to format the date object.

from datetime import datetime

timestamp = 1545730073
datetimeobj = datetime.fromtimestamp(timestamp).date()

output

datetime.date(2018, 12, 25)

if you want to format the date

datetimeobj.strftime("%Y-%m-%d")
//'2018-12-25'

Solution 2:[2]

import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  with open(filename, "w+") as file:
    pass
  timestamp = os.path.getmtime(filename)
  tm = datetime.datetime.fromtimestamp(timestamp).date()
  # Convert the timestamp into a readable format, then into a string
  
  # Return just the date portion 
  return ("{}".format(tm))

print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd

output date format : 2020-08-20

Solution 3:[3]

import datetime
from datetime import datetime


def file_date(filename):
  # Create the file in the current directory
  with open(filename,'w'):

    timestamp = os.path.getmtime(filename)
    date = datetime.fromtimestamp(timestamp).date()
  # Convert the timestamp into a readable format, then into a string

  # Return just the date portion
  # Hint: how many characters are in “yyyy-mm-dd”?
  return ("{}".format(date))

print(file_date("newfile.txt"))
# Should be today's date in the format of yyyy-mm-dd```

Solution 4:[4]

import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  with open(filename,'w'):
    timestamp = os.path.getmtime(filename)
    # Convert the timestamp into a readable format, then into a string
    date = datetime.datetime.fromtimestamp(timestamp).date()
    # Return just the date portion 
    # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(date))

print(file_date("newfile.txt")) 

Solution 5:[5]

import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  with open(filename, 'w') as file:
    pass
  timestamp = os.path.getmtime(filename)
  # Convert the timestamp into a readable format, then into a string
  time = datetime.datetime.fromtimestamp(timestamp)
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(str(time)[:10]))

print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd

Solution 6:[6]

Here is way!

    import os
    import datetime

    def file_date(filename):
  # Create the file in the current directory
      with open(filename,"w") as file:
        pass
      timestamp = os.path.getmtime(filename)
  # Convert the timestamp into a readable format, then into a string
      date = datetime.datetime.fromtimestamp(timestamp).date()
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”?  
      return ("{}".format(date))

    print(file_date("newfile.txt")) 
  # Should be today's date in the format of yyyy-mm-dd

Solution 7:[7]

2 ways:

1.

st = datetime.datetime.fromtimestamp(timestamp).date()
return ("{}".format(st));
st = datetime.datetime.fromtimestamp(timestamp)  
return ("{}".format(st.strftime("%Y-%m-%d")))

Solution 8:[8]

import os
from datetime import datetime

def file_date(filename):
    # Create the file in the current directory
    fp = open(filename, 'w')
    timestamp = os.path.getmtime(filename)
    fp.close()
    # Convert the timestamp into a readable format, then into a string
    readable = datetime.fromtimestamp(timestamp)
    string = readable.strftime('%Y-%m-%d %H:%M:%S')

    # Return just the date portion
    # Hint: how many characters are in “yyyy-mm-dd”?
    s1 = slice(10)
    return(string[s1])


print(file_date("newfile.txt"))
# Should be today's date in the format of yyyy-mm-dd

Output will be like 2022-02-12

Solution 9:[9]

import os
import datetime

def file_date(filename):
  # Create the file in the current directory
  open(filename, 'w')
  timestamp = os.path.getmtime(filename)
  # Convert the timestamp into a readable format, then into a string
  datet = datetime.datetime.fromtimestamp(timestamp).date()
  # Return just the date portion 
  # Hint: how many characters are in “yyyy-mm-dd”? 
  return ("{}".format(datet))

print(file_date("newfile.txt")) 
# Should be today's date in the format of yyyy-mm-dd

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rajith Thennakoon
Solution 2 Suhas Raj
Solution 3 aro01
Solution 4
Solution 5 Adel Abu Hashim
Solution 6 SSV 1
Solution 7 Tyler2P
Solution 8 Joeshibha
Solution 9 AKinsoji Hammed Adisa