How to get the current time in Python

前端 未结 30 1430
滥情空心
滥情空心 2020-11-22 06:47

What is the module/method used to get the current time?

30条回答
  •  -上瘾入骨i
    2020-11-22 07:23

    You can use this function to get the time (unfortunately it doesn't say AM or PM):

    def gettime():
        from datetime import datetime
        return ((str(datetime.now())).split(' ')[1]).split('.')[0]
    

    To get the hours, minutes, seconds and milliseconds to merge later, you can use these functions:

    Hour:

    def gethour():
        from datetime import datetime
        return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[0]
    

    Minute:

    def getminute():
        from datetime import datetime
        return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[1]
    

    Second:

    def getsecond():
        from datetime import datetime
        return (((str(datetime.now())).split(' ')[1]).split('.')[0]).split(':')[2]
    

    Millisecond:

    def getmillisecond():
        from datetime import datetime
        return (str(datetime.now())).split('.')[1]
    

提交回复
热议问题