A way to extract datetime.now format from line?

datetime format
datetime.parseexact format
python datetime now
pandas datetime format
datetime.parse format dd/mm/yyyy
convert string to date c# dd/mm/yyyy without time
date conversion in python

I have a log file that contains lines such as

### on 2018-06-19 23:57:37.846200 ###

### on 2018-06-19 23:50:33.073267 ###

It may also contain other data on different lines.

I am wanting to print all lines that begin with ### and extract their date/time so that I can compare them with another date/time.

How can I do this? Should I use Regex?

Here is an example of what I am doing...

    with open('myfile.log', 'r') as myfile:
        for line in myfile:
            if "###" in line:
                x = line

            # get date and time from x

except OSError as e:
    print (e)

You can use regex and the parse the captured group with datetime.striptime

This will allow you to edit the regex to match 1 or more whitespaces, etc.

from datetime import datetime
import re

with open('myfile.log') as myfile:
    for line in myfile:
        # Adding the ### into the regex allows you to remove the conditional if ### in line
        regex = r'###\s+\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}.+(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{0,})\s###'
        r = re.match(regex, line)
        if r:  # If there is a matching group, return matching group 1
            dt = datetime.striptime(r.group(1), "%Y-%m-%d %H:%M:%S.%f")


extract the date part from DateTime in C#, up vote 32 down vote. There is no way to "discard" the time component. DateTime.Today is the same as: DateTime d = DateTime.Now.Date; For example, using the standard format string "D" (long date format specifier): d. How to Display the Date and Time Using Linux Command Line Use format characters to display the date and time with precision

You can use regex in this type of problems

  with open('myfile.log', 'r') as myfile:
    reg = re.compile('^###.*on\s([\w\s.:-]*)')
    for line in myfile:
       m = reg.match(line)
       if m:
         datetime.striptime(m.group(1), "%Y-%m-%d %H:%M:%S.%f")

DateTime.ParseExact Method (System), public static DateTime ParseExact (string s, string format, IFormatProvider succeeds, the resulting DateTime value has a date of DateTime.Now.Date . If s does  A quick way to pick a decimal number representing a certain time is to use the Format Cells dialog. Simply select a cell containing the time and press Ctrl + 1 to open the Format Cells dialog box. On the Number tab, select General under Category, and you will see the decimal in the Sample box.

Assuming that a line has always the fixed format, then this should work:

# Extract the date and time substring.
s = x[-30:-4]

# Parse the string into a datetime object.
dt = datetime.datetime.strptime(s, "%Y-%m-%d %H:%M:%S.%f")

See the strptime() documentation for details on the template string.

If the format of a line in your logfile varies, then a regex might work (see Daniel’s answer), or consider properly parsing a string.

Python Datetime Tutorial: Manipulate Times, Dates, and Time Spans, It can extract the day of the week, day of the month, and other date and time formats from strings. In short, it's a really powerful way of handling anything date and time related in We can do this using datetime 's .now() function. Let's give it a date string in YYYY-MM-DD format and see what it can do! This is why the INT function will work to extract a Date from a Date-Time value. The integer value is a date serial number. The trick is to change the cell formatting to a Date format so that Excel will show you the Date. Format Cells Date. No related posts. This entry was posted in Intermediate, Time and Date on by .

Python : How to convert datetime object to string using datetime , Get the current timestamp in a datetime object i.e. Convert this datetime object to string in format 'DD-MMM-YYYY (HH:MM:SS:MICROS)' i.e. Python: How to unzip a file | Extract Single, multiple or all files from a ZIP archive  I'm trying to extract 2 dates from a string using regex- and for some reason - the regex doesn't extract dates- this is my code: private String[] getDate(String desc) { int count=0; Strin

C# DateTime Format, Format codes represent time, and dates, in different ways. Character codes. Now; string format = "MMM ddd d HH:mm yyyy"; Console.WriteLine(time. Best way to identify and extract dates from text Python? While these dates are in-line with natural text, none of them are in specifically natural language forms

How to get current date and time in Python?, Example 2: Current date in different formats. from datetime import date today = date.today()  ExtractNow is a simple utility that allows you to extract multiple archives quickly and easily.

  • Consider using if line.startswith("###") to check for "###" at the beginning of the line, instead of anywhere in the line.
  • I would prefer naming the group in the regex r"…(?P<dt>...)…" and then referring to it by name rather than offset: r.group("dt"). I think that’s a bit easier to read and maintain.
  • Naming groups is certainly useful. But when there is only one group the offset is easy to remember. And naming the group for someone who doesn't understand regex just makes the regex harder to comprehend
  • Please fix indent and your code. Also, to improve performance, consider compiling the regex before the loop, and then use the compiled object to match the string. Also, remember that search() can return None, so that needs to be checked too.
  • I am getting ValueError: time data '018-06-20 09:12:30.360350 ' does not match format '%Y-%m-%d %H:%M:%S:%f'
  • Yes, your log date string misses the 2 at the beginning, and your template string contains :%f instead of .%f.