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

Related

powershell, csv-date imported in different formate

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)

How to convert a string into datetime with format directives from another Timezone with Python 3

I have function in my Django application that pulls informations from SSH. I get a raw date string like the following (French language):
'mar. 27 mars 2012 17:51:42 CEST'
My issue is that I need to convert this string to a datetime object before passing it to update one of my model, but I get an error.
ssh_output = 'mar. 27 mars 2012 17:51:42 CEST'
my_datetime = datetime.datetime.strptime(ssh_output,"%a. %d %B %Y %H:%M:%S CEST")
Above code throws an exception:
time data 'mar. 27 mars 2012 17:51:42 CEST' does not match format '%a.
%d %B %Y %H:%M:%S CEST'
I'm confused because locale.getlocale() outputs: "
('fr_FR', 'UTF-8')
But testing datetime format with datetime.datetime.now().strftime("%a %d %B %Y %H:%M:%S %Z") outputs (English language):
Wed 08 July 2020 15:46:11
Also, all date format directives seems correct (plus literal strings '.' and 'CEST'):
%a : Weekday as locale’s abbreviated name.
%d : Day of the month as a zero-padded decimal number.
%B : Month as locale’s full name.
%Y : Year with century as a decimal number.
%H : Hour (24-hour clock) as a zero-padded decimal number.
%M : Minute as a zero-padded decimal number.
%S : Second as a zero-padded decimal number.
So it seems that raw datetime string grabbed from SSH output 'mar. 27 mars 2012 17:51:42 CEST' can't be parsed/converted because it does not match current timezone...
How can I convert this raw datetime string into datetime in this case?
To parse CEST to the correct timezone, you could split the ssh output into the datetime part and the timezone part. After that you can apply a mapping dict:
import datetime
import dateutil
import locale
locale.setlocale(locale.LC_ALL, 'fr_FR')
ssh_output = 'mar. 27 mars 2012 17:51:42 CEST'
# assuming timezone abbreviation separated by space
parts = ssh_output.split(' ')
timestr, tzstr = ' '.join(parts[:-1]), parts[-1]
# create a custom mapping dict
tzmapping = {'CEST': dateutil.tz.gettz('Europe/Paris')}
my_datetime = datetime.datetime.strptime(timestr,"%a %d %B %Y %H:%M:%S")
my_datetime = my_datetime.replace(tzinfo=tzmapping[tzstr])
# datetime.datetime(2012, 3, 27, 17, 51, 42, tzinfo=tzfile('Europe/Paris'))

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'.

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());

Format the time from moment js

When I am trying to receive mail from gmail, I get time in this format (Mon, 12 Jun 2017 10:29:07 +0530). I want to calculate time minus current time. How to do so?
var testDate = moment("Mon, 12 Jun 2017 10:29:07 +0530");
//Relative to time in human readble format
testDate.fromNow(); //3 days ago
You can simply call the moment constructor with the given Date format. moment.js is smart enough to parse it for you. To get the difference you can convert it into unix based time format and subtract it.
const givenTime = moment("Mon, 12 Jun 2017 10:29:07 +0530").unix()
const currentTime = moment().unix()
//Difference in milliseconds
const diff = givenTime - currentTime

Resources