Converting string to timestamp? - string

I have a xml file in which date is represented as this: "2010-07-10T14:46:00.000Z"
Im parsing it and fetching it as string.
I need to convert it into timestamp or date format to store in db. How do i do it?
Please help.

Assuming that you use PHP:
You can use the function DateTime::createFromFormat(string $format, string $time)
try the following:
$timestamp = str_ireplace(array('T', 'Z'), '', "2010-07-10T14:46:00.000Z");
$datetime = DateTime::createFromFormat('j-m-d H:i:s.u ', $timestamp);
echo $date->getTimestamp();
Please see the documentation of DateTime::createFromFormat and date() for the correct format string.

generally you need to use strptime() related functions, that come from standard C library.

Related

Convert string to datetime in Powershell using ParseExact

I have a Csv file with 3 strings for DateTime.
Date;
202202230930;
202202220815;
202202220612;
Because I have to use the Date for naming Snapshots on Proxmox i cant use the - sign. I wanna convert these Dates in to yyyy-MM-dd HH:mm
Now i wanna create a datetime variable in Powershell using that csv file. With the format 'yyyy.MM.dd HH:mm'
I created a foreach so for each date it will ParseExact the string into a datetime.
$datesnapshot = import-csv -Path "D:\autosnap\Datetime.csv" -Delimiter ";"
Foreach($Date in $datesnapshot){
[datetime]::ParseExact($datesnapshot, 'yyyyMMddHH:mm', $null).ToString('yyyy-MM-dd HH:mm')
}
I get the error "String was not recognized as a valid DateTime." I don't know where the problem is because i typed in the Date it is expected to parse and the date i wanna receive. I hope somebody can help.
Kind regards
A few things to note, the first one:
[datetime]::ParseExact($datesnapshot...
You're iterating over the collection $datesnapshot but also you're giving ParseExact the same collection as argument, it should be $date (the item) instead. In addition, your CSV generates an array of objects (object[]), where each object has a property with name Date. If you want to reference the value of your objects you should be using $date.Date. See about_Properties for more info.
Lastly, the format you're yyyyMMddHH:mm using will not correctly parse your dates. The format you should use is yyyyMMddHHmm.
$datesnapshot = #'
Date;
202202230930;
202202220815;
202202220612;
'# | ConvertFrom-Csv -Delimiter ';'
foreach($Date in $datesnapshot){
[datetime]::ParseExact($Date.Date, 'yyyyMMddHHmm', [cultureinfo]::InvariantCulture).ToString('yyyy-MM-dd HH:mm')
}

Python format incomplete date to YYYYMM

As a start, I am extremely new at Python.
I am receiving an Excel file where the date field is incomplete. The value displays as "190808" (YYMMDD) instead of "2019-08-08".
Part of my automation attempt is to move the file to a different location, where the file is renamed. I want to use the date field to change the file name to the file description and date (e.g. "Sales figures 201908").
The code I have only works if the date format is
str(df['Bank date'][0].strftime("%Y%m"))
I have tried dateparser with the following:
dateparser.parse(df['Bank date'][0].strftime("%Y.%m"))
The error I am receiving is 'numpy.int64' object has no attribute 'strftime'
Any help will do.
Thanks.
I modified it slightly and built my own date-string using slicing.
vOldDate = str(df['Bank date'][0])
vNewDate = '20' + vOldDate[:2] + '.' + vOldDate[2:4]
Numpy is interpreting the date as an integer. To use dateparser, you need to convert that value into a string first, then parse that string, and then format the result:
dateparser.parse(str(df['Bank date'][0])).strftime("%Y.%m")
Since the input format is expected, you should specify it to ensure you get the right date:
>>> dateparser.parse(str(190808), date_formats=['%y%m%d']).strftime("%Y.%m")
'2019.08'

Extracting substring in powershell using regex

I have a string in excel that I need to extract a substring from
This is an example of the string:
<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>
I'm new to regex and powershell, but I'm trying to find a way to extract the "hostname here" portion of the string. It's variable length, so indexing won't be reliable.
since you changed the sample, the comment code i posted won't work. [grin] this will, tho ...
$InStuff = '<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>'
$InStuff.Split(':')[-1].Split('<')[0].Trim()
output = hostnamehere
if you have a set of sample strings, then you likely otta post them so the code can be arranged to handle the needed variants.
If that were xml, it would be straightforward
[xml]$xml = '<Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue></Text>'
(-split $xml.text.textvalue)[1]
hostnamehere

How to convert a String to Datetime using Dataweave Mule

I have the following string 20170912113456 and i want to convert it to this format 2017/09/12 11:34:56 using Dataweave Mule.
Any ideas about how to do it ?
I would recommend first converting your input date string into a Dataweave localdatetime object. Then you can output the date object back to a string of any format.
%dw 1.0
%output application/json
---
formattedDate: flowVars.date as :localdatetime{format: "yyyyMMddHHmmss"} as :string{format:"yyyy/mm/dd HH:mm:ss"}
If you interested in learning the inner-working check out my converting string dates to Java Date Objects in Mule tutorial, https://youtu.be/XqI_Kii9RzA
You can try few things like formatting to data and then to string based on below example.
myDate: ((payload as :string) as :date {format: "yyyyMMdd"}) as :string {format: "MM-dd-yyyy"}
For a better understanding the Date Time operations please go through below link.
https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-operators#date-time-operations

AwesomeWM time not updating?

I'm using AwesomeWM, and I'm trying to display the time in my wibox using this code
vicious.register(datewidget, vicious.widgets.date, os.date("%b ")..(os.date("%d")+0).. ', ' ..(os.date("%I")+0)..os.date(":%M")..string.lower( os.date(" %p ")), 1)
The time is correct when I open AwesomeWM, but it doesn't update. For whatever reason , 1) doesn't work.
heres my full rc.lua
I guess the problem is with what the register function expects. It expects a format string with which it can calculate date itself. Here you're passing a literal string instead of formatting parameters.
From your older question, I found a different method for the same. Now, your vicious need to be like:
vicious.register(datewidget, vicious.widgets.date, "<span font-family='terminus' color='#999999'>%b %d, %l:%M %P</span>", 1)
And it should work.
P.S. Thanks to sa1

Resources