Convert string to datetime in Powershell using ParseExact - string

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')
}

Related

Convert string to date format in azure data factory using set variable

I have string date in set variable "20211222"
And I want to convert it into date like 2021-12-22.
And I have use this function in variable set dynamic content
#formatDateTime('20211222', 'yyyy-MM-dd')
But error occur In function 'formatDateTime', the value provided for date time string '20211222' was not valid. The datetime string must match ISO 8601 format
Is there any other function to convert this string "20211222" into date?
Actually the string '20211222' is already in the unambiguous format of YYYYMMDD and will always be interpreted this way. If you need to use this string input as a date, just do a cast:
SELECT CAST('20211222' AS date); -- 2021-12-22
If you wanted to formerly go from your text input YYYYMMDD to a text output of YYYY-MM-DD, then make a round trip:
SELECT CONVERT(varchar(10), CAST('20211222' AS date), 120);
Function formatDateTime expects "a string that contains the timestamp".
Example:
formatDateTime('03/15/2018 12:00:00', 'yyyy-MM-ddTHH:mm:ss')
You would have to manage to input in a timestamp format. The default format for the timestamp is "o" (yyyy-MM-ddTHH:mm:ss:fffffffK), which complies with ISO 8601 and preserves time zone information.
Please use the below logic:
#concat(substring(pipeline().parameters.Test,0,4 ),'-',substring(pipeline().parameters.Test,4,2),'-',substring(pipeline().parameters.Test,6,2))

Converting Issue date with download to CSV

I have a problem with the Function Module "GUI_DOWNLOAD" because of the date converting.
I want to get the date like I have it in my internal table but CSV (Excel) keeps converting it everytime.
The internal table contains the line like this: 12345678;GroupDate;2021-12-31;
The Output in the .csv-File should be "2021-12-31" but it keeps converting to "31.12.2021".
I also tried to put an ' (apostroph) before the date but the output will be '2021-12-31
Does anybode have an Idea ?
lv_conv = '2021-12-31'.
CONCATENATE TEXT-001 LV_CONV INTO LV_CONV.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = IV_PATH
TABLES
DATA_TAB = LT_FILE.
LT_FILE is a string table.
Thanks for the help.
Like Suncatcher and Sandra said the file is right but it is only the settings from excel which convert the date.
If the Output file won´t be needed for other purposes than showing the code could be something like this
CONCATENATE '=("' LV_CONV '")' INTO LV_CONV.
The csv-Output would be a date like this 1960-01-01 but in the cell the value would look like =("1960-01-01").

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'

Create a new PowerShell string containing defined substrings

I've found myriad methods to retrieve data FROM a string with substrings, but what I want to do is create a new string that contains substrings. The reason for this is that I want to pass that string to a CSV through the Export-CSV cmdlet. This is in a PowerShell Forms created app.
So the plan would be to
1). Read the contents of each text box:
(e.g. $endusername.text $endusernumber.text $locationname.text)
2). Store those into a new string with substrings
($formoutput.endusername $formoutput.endusernumber $formoutput.locationname)
3). Output the string to a .CSV
Export-CSV -InputObject $formoutput "c:\output\formoutput.csv"
Basically, if I take any existing cmdlet (say, Get-Mailbox), store its output as a string, and then pass that string through the Export-CSV in the way explained above, it performs exactly the way I like - creating a .CSV with each of the substrings as a column, and the contents of that substring in the appropriately headed column. I just want to be able to do that with a string containing substrings that I define.
I think you are confusing nomenclature a little bit. It sounds like what you want is a custom object not a string. Here is some pseudo-code to get you going in the right direction:
$formOutput = New-Object PsCustomObject -Property #{'EndUserName' = $endUserName.Text;
'EndUserNumber' = $endUserNumber.Text;
'LocationName' = $locatioName.Text}
$formOutput | Export-CSV .\FileName.csv -NoTypeHeader

Converting string to timestamp?

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.

Resources