How to get the week day name from a date ?

How to get the week day name from a date ?

Asked on December 27, 2018 in Python.
Add Comment


  • 3 Answer(s)

    One can simply use a normal expression or function to extract the date and “datetime.datetime.strptime” to parse the date, But a condition is the date which is given in a fixed form. Use this code,

    match = re.search(r'\d{4}-\d{2}-\d{2}', text)
    date = datetime.strptime(match.group(), '%Y-%m-%d').date()
    
    Answered on December 27, 2018.
    Add Comment

    Try to use python-dateutil, Have a look at the code.

    In [1]: import dateutil.parser as dparser
     
    In [18]: dparser.parse("monkey 2010-07-10 love banana",fuzzy=True)
    Out[18]: datetime.datetime(2010, 7, 10, 0, 0)
    

    ValueError: Invalid dates raise.

    In [19]: dparser.parse("monkey 2010-07-32 love banana",fuzzy=True)
    # ValueError: day is out of range for month
    

    This one can acknowledge dates in more formats:

    In [20]: dparser.parse("monkey 20/01/1980 love banana",fuzzy=True)
    Out[20]: datetime.datetime(1980, 1, 20, 0, 0)
    

    If the date is equivocal,  it makes a guess :

    In [23]: dparser.parse("monkey 10/01/1980 love banana",fuzzy=True)
    Out[23]: datetime.datetime(1980, 10, 1, 0, 0)
    

    The way it parses equivocal dates is customizable:

    In [21]: dparser.parse("monkey 10/01/1980 love banana",fuzzy=True, dayfirst=True)
    Out[21]: datetime.datetime(1980, 1, 10, 0, 0)
    
    Answered on December 27, 2018.
    Add Comment

    There is a one of the module available named datefinder module. It can be used for extracting the date from a string in Python.The way of use it.

    Step 1: Install datefinder Package

    pip install datefinder
    

    Step 2: Use It In the Project

    import datefinder
     
    input_string = "monkey 2010-07-10 love banana"
    # a generator will be returned by the datefinder module. I'm typecasting it to a list. Please read the note of caution provided at the bottom.
    matches = list(datefinder.find_dates(input_string))
     
    if len(matches) > 0:
        # date returned will be a datetime.datetime object. here we are only using the first     match.
        date = matches[0]
        print date
    else:
        print 'No dates found'
    
    Answered on December 27, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.