Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime 'has no attribute now' [duplicate]

I'm trying to write a simple program to print the current date with Python 3.4. In the shell, I can import datetime, and use now() but when I write a script with a class it fails and gives this error:

"AttributeError: module object has no attribute now". 

Can anyone help explain the problem? This is my code:

import datetime

class Date:
    def __init__(self, filename):
        self.writeToFile(filename)

    def date(self):
        now = datetime.datetime.now()
        return now

    def writeToFile(self, filename):
        date = self.date()

        file = open(filename, 'w')
        file.write(date)
        for i in range(20):             # simply test for writting in file
            file.write(str(i)+'\t')
        file.close()
        return file

d = Date('datetime.txt') 
like image 393
MartinT_25 Avatar asked Aug 26 '26 13:08

MartinT_25


1 Answers

import datetime
datetime.datetime.now()
like image 194
Ibrahim Tayseer Avatar answered Aug 28 '26 04:08

Ibrahim Tayseer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!