Can not parse time string with pd.to_datetime - python-3.x

Not able to parse a date string into a Pandas date.
I have tried various formats for the date, however, I can't figure out a way to easily debug it as opposed to guessing.
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019', format=('%A %B %d %H:%M:%S','+0000', '%Y'))
ValueError: time data 'Thu Mar 21 18:24:35 +0000 2019' doesn't match format specified

For certain versions of pandas (I tested with 0.24.1), it should work without specifying format:
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019')
There are, however, issues with your format string. For one, I'm not sure why you're provided a tuple rather than one continuous string. For two, you use '%A' and %B' - in datetime formatting, these provide the weekday and month as the full name in both cases (source). In your case, you have the abbreviated names - '%a' and '%b'. So the following should work:
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019', format='%a %b %d %H:%M:%S +0000 %Y')

Related

Reading date in excel file through ExcelJS give wrong date

I am working at EST timezone. When I reading a date in Excel file with the help of ExcelJS. The date in Excel is 3/31/2021. But the same date is retrieving as Tue Mar 30 2021 20:00:00 GMT-0400 (Eastern Daylight Time). So it is considering the given date as UTC and converting it to my current timezone. I dont want to convert it to UTC and then to my current timezone. I want to read the direct date which is present in the EXCEL file.
I see dateUTC: false somewhere in the document. This is used at Writing the file. But how to do it when I am reading the excel file. Any help on this?
First I converted the GMT date to milliseconds using Date.parse() and added the time difference between GMT and the current time zone (in my case Asia / Tashkent +5) to the resulting value, then converted it back to a regular date using new Date(milliseconds)
let m = Date.parse(gmtDate); //GMT date in milliseconds
let mTZ = m + 18000000; //Asia/Tashkent +5 time zone. 18.000.000 = 5 hours in milliseconds.
let currentDate = new Date(mTZ);
I ran into this recently and had to do the same thing:
Read from excel as UTC
then convert into the timezone I wanted for data
I know from your question you don't want to do this, so perhaps this plan is something to fall back on.
My code was something like this:
const moment = require('moment-timezone')
let utc = moment.utc(excelCellStr, 'M/D/YY hh:mm A')
let t = moment().tz('America/New_York')
t.year(utc.year())
t.month(utc.month())
t.date(utc.date())
t.hour(utc.hour())
t.minute(utc.minute())
t.second(utc.second())
excelCellStr = t.format()
I am not sure if there is a setting to avoid this or not. We are using exceljs as well.

Freemarker ISO string to datetime with timezone

I need to display the below "String" in the desired format
String str = 1979-01-24T00:00:00.000-08:00
Desired format: Jan 24, 1979 00:00:00 AM PST
Note: The tz in the str could be any tz not limited to PST.
Tried the below but none worked:
str?datetime.iso - Output is Jan 24, 1979 2:00:00 AM CST - This displays the date time in the format I need but the time is being converted from PST to CST.
str?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Expected a method, but this has evaluated to a string
str?datetime?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Unparseable date: "1979-01-24T00:00:00.000-08:00"
<#setting datetime_format="iso"> str?datetime - 1979-01-24T02:00:00-06:00 - The timezone is changed.
The problem here is that FreeMarker parses date/time values to java.util.Date (and its subclasses), which don't store a time zone anymore, as it always stores the value in UTC. So that information is lost after parsing. As of 2.3.30, the only solution I see to do this in Java (with Java 8 ZonedDateTime).
The timezone can be configured by the following setting, as refer to their documentation https://freemarker.apache.org/docs/ref_directive_setting.html
<#setting time_zone ="PST">
<#assign str = "1979-01-24T00:00:00.000-08:00">
${str?datetime.iso}

Convert date & time string to date & time format in Bash

I have a log file which contains entries in this format:
Feb 15 14:28:37 [8085][8095] ssnotify.cpp:442:Send().....
How can I convert the date and time string to a date/time format so I can use it with other date/time commands within a bash script?
You can use the "date" command for this task.
Example:
user#host:so$ date --date="Feb 15 14:28:37"
jue feb 15 14:28:37 CET 2018
user#host:so$ date --date="Feb 15 14:28:37" +%s
1518701317
If you do: "man date" you will see all the options about how to format the date as you wish.

How can I convert date to a readable form in Angular and also concatenate two dates?

For any event, Im getting dates in my html file in the following manner:
Start Date: 2017-09-25T10:00:00
End Date: 2017-09-25T11:00:00
In my Controller I have them saved as :
Event= {StartDate: start, EndDate: end}
I am looking for a way to output them in my html with some concatenation and trimming done to the date. My event date will always be the same so I would like to be displayed something like:
September 09 2017, 10 AM - 11:00 AM
Help Needed!
Here's a working plunker that will give you a feel for the date formatting you're after: https://plnkr.co/edit/tmxQY9RyqGzHwCKhvQN0?p=preview
The template interpolation is key here
{{Event.StartDate | date:"MMMM dd yyyy ', ' h"}} {{Event.EndDate | date: "'-' h:mm a"}}
Note: you could save yourself from parsing the left side of the "-" with Angular's 'longDate', but that will add a comma after the day of the month (e.g. September 20, 2017 vs September 20 2017).
Check out the docs for detailed info on which date parsing character does what.
Just use JavaScript, you have the split function (ex: .split(/[-T]+/)). For the month you have the 09 number, create your own function to get the month by array's indexes.

Making Excel Recognize Custom Day/Time Format

I'm working with a date and time format from the Twitter API. It looks like this:
Tue Nov 26 20:44:15 +0000 2013
Is there a formula to convert this to a format that could be sorted chronologically? I don't need the +0000. Also not concerned about the day of week.
=DATEVALUE(MID(A1,9,3) & MID(A1,4,5) & RIGHT(A1,4)) + TIMEVALUE(MID(A1,11,9))
Then format as you like. As requested you would want a Custom Format of mmm dd HH:mm yyyy
Try this, assuming that your string is in A1:
=DATETIME(MID(A1, 5, 16) & MID(A1, 27, 4))
The two MID formulas cut out the parts of the string you want, namely the month, day, time, and year, but exclude the day of the week and the timezone. This produces a string that the DATETIME function can automatically recognize and covert into a native Excel date format.

Resources