powershell, csv-date imported in different formate - excel

I want to import this kind of csv into Excel
Work Item Type,ID,State,Date Request,Created Date
"Task","4533","Closed","2-9-2020 14:26:00","3-9-2020 08:17:39"
"Task","4535","Closed","3-9-2020 12:26:44","3-9-2020 12:29:33"
"Task","4577","Closed","3-9-2020 15:56:00","4-9-2020 09:12:21"
"Task","4580","New","17-8-2020 09:47:00","4-9-2020 09:49:39"
"Task","4581","Resolved","28-8-2020 10:22:00","4-9-2020 10:24:46"
"Task","4582","Resolved","24-8-2020 10:05:00","4-9-2020 10:31:12"
"Task","4604","Resolved","8-9-2020 08:06:58","8-9-2020 08:07:23"
"Task","4605","Resolved","8-9-2020 09:18:32","8-9-2020 09:18:58"
All dates in this example must be seen with a format day-month-year hour:minute:second
I do the import like this:
Import-Csv -Path '.\Issues.csv' | ForEach-Object {
$sheet1.Cells.Item(1,1) = 'ID'
$sheet1.Cells.Item(1,2) = 'Status'
$sheet1.Cells.Item(1,3) = 'Date Request'
$sheet1.Cells.Item(1,4) = 'Date Created'
$DateRequest = ([datetime]::ParseExact(($($_."Date Request")),$fmtDate,$inv).ToString($fmtDate))
$sheet1.Cells.Item($row,1) = $($_.ID)
$sheet1.Cells.Item($row,2) = $($_.State)
$sheet1.Cells.Item($row,3) = $($_."Date Request")
$sheet1.Cells.Item($row,4) = $($_."Created Date")
$row = $row + 1
}
The result of my Import
ID Status Date Request Date Created
4533 Closed 9/02/2020 14:26 9/03/2020 8:17
4535 Closed 9/03/2020 12:26 9/03/2020 12:29
4577 Closed 9/03/2020 15:56 9/04/2020 9:12
4580 New 17-8-2020 09:47:00 9/04/2020 9:49
4581 Resolved 28-8-2020 10:22:00 9/04/2020 10:24
4582 Resolved 24-8-2020 10:05:00 9/04/2020 10:31
4604 Resolved 9/08/2020 8:06 9/08/2020 8:07
4605 Resolved 9/08/2020 9:18 9/08/2020 9:18
As you can see, some dates are red in the CSV with a month-day-year format,
other are red with a day-month-year format.
The date 3 september has become 9 march
I have tried using CultureInfo, but without any succes.
$inv = [System.Globalization.CultureInfo]::InvariantCulture<br>
$fmtDate = "dd/MM/YYYY HH:mm:ss"
$DateRequest = ([datetime]::ParseExact(($($_."Date Request")),$fmtDate,$inv).ToString($fmtDate))
Does anyone hove any suggestions to solve this?

First of all, the dates in your CSV file have this format d-M-yyyy HH:mm:ss (yyyy is in lowercase and the days and months in the fields do not have a leading zeroes).
Try
$fmtDate = "d-M-yyyy HH:mm:ss"
What puzzles me is why you want to first parse the date in the csv and then use ToString() to reformat it in the exact same string format.
Take off the .ToString($fmtDate) as in
$DateRequest = [datetime]::ParseExact($_."Date Request",$fmtDate, $inv)
and feed that DateTime object into the Excel cell

dd/MM/YYYY HH:mm:ss does NOT describe the input date format you have - dd and MM are for day and month numbers with leading zeros.
Use:
$fmtDateInput = 'd-M-yyyy HH:mm:ss'
$fmtDateOutput = "dd/MM/yyyy HH:mm:ss"
[datetime]::ParseExact($dateString, $fmtDateInput, $culture).ToString($fmtDateOutput)

Related

Time data with gmt does not match format input for pandas conversion

How to convert date time string with GMT into a pandas date time format ?
Here is an example :
#date_time is like 12/Dec/2015:18:25:11 +0100
df['date_time'] = pd.to_datetime(df['date_time'], format="%d/%b/%Y:%I:%M:%S %Z")
Here is the error :
ValueError: time data '12/Dec/2015:18:25:11 +0100' does not match
format '%d/%b/%Y:%I:%M:%S %Z' (match)
You'd better check the formatted string:
https://docs.python.org/3/library/datetime.html
Use '%d/%b/%Y:%H:%M:%S %z' instead of '%d/%b/%Y:%I:%M:%S %Z'.

To check if the continuity of dates are missing in a column

I want to check in my dataframe's column that if there is a missing date for a certain month then the code should output the following month in the format MMM- YYYY
The data set looks like this :
date_start_balance date_end_balance start_balance
22.02.16 22.03.16 3590838
22.04.16 22.05.16 69788
15.06.16 21.07.16 452165
Both date cols are in datetime format. Now in the above data set the dates are missing for March and May in the start col and this should be returned as MMM-YYYYY
I have tried the following code :
import datetime
dates = df1['date_start_balance'].tolist()
missing = []
for i in range(0,len(dates)-1):
if dates[i+1].month - dates[i+1].month != 1:
for j in range(dates[i].month+1,dates[i+1].month):
missing.append(datetime(dates[i].year, j,1))
print(missing)
You can first create a date range with pd.date_range
march = pd.date_range(start='2016-05-01', end='2016-05-31')
And then you will have the list with the dates that you already have, in the example there is only one date: 2016-05-15:
your_list = [datetime.datetime.strptime('15052016', "%d%m%Y").date()]
And then you can calculate the difference between the range and your list and get the dates that you are missing:
march.difference(your_list)
DatetimeIndex(['2016-05-01', '2016-05-02', '2016-05-03', '2016-05-04',
'2016-05-05', '2016-05-06', '2016-05-07', '2016-05-08',
'2016-05-09', '2016-05-10', '2016-05-11', '2016-05-12',
'2016-05-13', '2016-05-14', '2016-05-16', '2016-05-17',
'2016-05-18', '2016-05-19', '2016-05-20', '2016-05-21',
'2016-05-22', '2016-05-23', '2016-05-24', '2016-05-25',
'2016-05-26', '2016-05-27', '2016-05-28', '2016-05-29',
'2016-05-30', '2016-05-31'],
dtype='datetime64[ns]', freq=None)

convert string to ISO date time in nodejs

Convert this date into ISO format in nodejs
created_at="September 17th 2019, 16:50:17.000";
let new_time = new Date(created_at);
created_at = new_time.toISOString();
console.log(created_at);
Output: Invalid Date
Exacting output is in ISO format. like this 2011-10-05T14:48:00.000Z
Moment.js is a library which you can use to get the output and do some advanced operations with date and timezones.
Below is the code to get expected output.
var moment = require('moment')
created_at="September 17th 2019, 16:50:17.000";
let new_time = moment("September 17th 2019, 16:50:17.000", "MMMM Do YYYY, HH:mm:ss:SSS");
created_at = new_time.toISOString();
console.log(created_at);
You will have to pass the date string in below format in order to convert it to ISO date:
var date1 = "September 17 2019, 16:50:17.000";
let new_date = new Date(date1);
console.log(new_date);
console.log(new_date.toISOString());

How to change a given date in "yyyy-MM-dd HH:mm:ss.SSS" format to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" format in groovy

How to convert a given date in yyyy-MM-dd HH:mm:ss.SSS format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format in groovy
For example, the given date is 2019-03-18 16:20:05.6401383. I want it to converted to 2019-03-18T16:20:05.6401383Z
This is the code Used:
def date = format1.parse("2019-03-18 16:20:05.6401383");
String settledAt = format2.format(date)
log.info ">>> "+*date*+" "+*settledAt*
The result, where the date is getting changed somehow: Mon Mar 18 18:06:46 EDT 2019 & 2019-03-18T18:06:46.383Z
Thanks in advance for all the answers.
If you're on Java 8+ and Groovy 2.5+, I would use the new Date/Time API:
import java.time.*
def date = LocalDateTime.parse('2019-03-18 16:20:05.6401383', 'yyyy-MM-dd HH:mm:ss.nnnnnnn')
String settledAt = date.format(/yyyy-MM-dd'T'HH:mm:ss.nnnnnnn'Z'/)
This is presuming the input date has a "Zulu" time zone.
it's a feature of java
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS","2019-03-18 16:20:05.6401383")
returns
Mon Mar 18 18:06:46 EET 2019
the problem that java handles only milliseconds SSS (3 digits after seconds)
but you are providing 7 digits for milliseconds 6401383
as workaround remove extra digits with regexp:
def sdate1 = "2019-03-18 16:20:05.6401383"
sdate1 = sdate1.replaceAll( /\d{3}(\d*)$/, '$1') //keep only 3 digits at the end
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",sdate1)
def sdate2 = date.format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

Date of 7 days before today

I give current day in a string in MATLAB. For example if today is '20180703', I need 7 strings containing:
'20180702'
'20180701'
'20180630'
'20180629'
'20180628'
'20180627'
'20180626'
Simple:
t = datetime('20180703', 'InputFormat', 'yyyyMMdd')
t = t - days(1:7)
datestr(t, 'yyyymmdd')
Edit.
As excaza pointed out, datetime and datestr use different input format. Hence, 'MM' in the first function, and 'mm' in the second one.
I would go with something like:
lastSevenDays = arrayfun(#(offset) datestr(now-offset, 'yyyymmdd'), 1:7, 'UniformOutput', false)
or more matlaby:
datestr(now - days(1:7), 'yyyymmdd')

Resources