datetime.replace()方法


Python datetime.replace()方法 (Python datetime.replace() Method)

datetime.replace() method is used to manipulate objects of datetime class of module datetime.

datetime.replace()方法用於操作模塊datetime的datetime類的對象。

It is used to replace the date and time with the same value, except for those parameters given new values by whichever keyword arguments are specified in the brackets. It is an instance method which means that it works on an instance of the class.

它用於用相同的值替換日期和時間,但括號中指定的關鍵字參數為這些參數賦予新值的參數除外。 這是一個實例方法,這意味着它可以在類的實例上工作。

Module:

模塊:

    import datetime 

Class:

類:

    from datetime import datetime 

Syntax:

句法:

  1.  
    replace(
  2.  
    year=self.year,
  3.  
    month=self.month,
  4.  
    day=self.day,
  5.  
    hour=self.hour,
  6.  
    minute=self.minute,
  7.  
    second=self.second,
  8.  
    microsecond=self.microsecond,
  9.  
    tzinfo=self.tzinfo,
  10.  
    * fold= 0)

Parameter(s):

參數:

  • year: new year value of the instance (range: 1 <= year <= 9999)

    year :實例的新年份值(范圍:1 <= year <= 9999)

  • month: new month value of the instance (range: 1 <= month <= 12)

    month :實例的新月份值(范圍:1 <= month <= 12)

  • day: new day of the instance (range: 1<= day <= 31)

    day :實例的新日期(范圍:1 <= day <= 31)

  • hour: in range(24)

    小時 :在范圍內(24)

  • minute: in range(60)

    分鍾 :在范圍內(60)

  • second: in range(60)

    秒 :范圍(60)

  • microsecond: in range(1000000)

    微秒 :范圍(1000000)

  • tzinfo: object passed as the tzinfo argument to the datetime constructor, or None if none was passed.

    tzinfo :作為tzinfo參數傳遞給datetime構造函數的對象,如果沒有傳遞則為None。

  • fold: [0,1]

    倍數 :[0,1]

Return value:

返回值:

The return type of this method is a datetime class object after replacing the parameters.

替換參數后,此方法的返回類型是datetime類對象。

If values are not in the given range a ValueError is raised.

如果值不在給定范圍內,則會引發ValueError 。

Example:

例:

  1.  
    ## Python program explaining the
  2.  
    ## use of datetime class instance methods
  3.  
     
  4.  
    from datetime import datetime
  5.  
    import pytz
  6.  
     
  7.  
    ## Creating an instance
  8.  
    x = datetime( 2019, 9, 25,4,54,23)
  9.  
    print("Datetime entered was:", x)
  10.  
    print()
  11.  
     
  12.  
    x = datetime.now()
  13.  
    print("Today's date and time:", x)
  14.  
     
  15.  
    ## Using replace() method
  16.  
    d = x.replace(year = 2022)
  17.  
    print("New date after changing the year:", d)
  18.  
    print()
  19.  
     
  20.  
    d = x.replace(month= 1)
  21.  
    print("The date after changing the month:", d)
  22.  
    print()
  23.  
     
  24.  
    d = x.replace(day= 3)
  25.  
    print("The date after changing the day:", d)
  26.  
    print()
  27.  
     
  28.  
    d = x.replace(year= 2025, day=30)
  29.  
    print("The date after changing the day and year:", d)
  30.  
    print()
  31.  
     
  32.  
    d = x.replace(year= 1999, month =12, day=3)
  33.  
    print("The date after changing the year, month and day:", d)
  34.  
    print()
  35.  
     
  36.  
    d = x.replace(hour = 12)
  37.  
    print("The date after changing the hour:",d)
  38.  
    print()
  39.  
     
  40.  
    d = x.replace(minute= 4)
  41.  
    print("The date after changing the minute attribute:",d)
  42.  
    print()
  43.  
     
  44.  
    d = x.replace(year= 2220, month=10, day=28, hour=21, minute =5, second = 20)
  45.  
    print("The date after the changes:",d)
  46.  
    print()
  47.  
     
  48.  
    timezone = pytz.timezone( "Asia/Kolkata")
  49.  
    d = x.replace(tzinfo=timezone)
  50.  
    print("The date after changing the tzinfo:",d)

Output

輸出量

  1.  
    Datetime entered was: 2019-09-25 04:54:23
  2.  
     
  3.  
    Today's date and time: 2020-04-30 19:11:10.683769
  4.  
    New date after changing the year: 2022-04-30 19:11:10.683769
  5.  
     
  6.  
    The date after changing the month: 2020-01-30 19:11:10.683769
  7.  
     
  8.  
    The date after changing the day: 2020-04-03 19:11:10.683769
  9.  
    The date after changing the day and year: 2025-04-30 19:11:
  10.  
    10.683769
  11.  
    The date after changing the year, month and day: 1999-12-03
  12.  
    19:11:10.683769
  13.  
     
  14.  
    The date after changing the hour: 2020-04-30 12:11:10.683769
  15.  
     
  16.  
    The date after changing the minute attribute: 2020-04-30 19
  17.  
    :04:10.683769
  18.  
     
  19.  
    The date after the changes: 2220-10-28 21:05:20.683769
  20.  
     
  21.  
    The date after changing the tzinfo: 2020-04-30 19:11:10.683
  22.  
    769+05:53
  23.  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM