Convert the parsed T&Z timestamp to millisecond - groovy

1) In response, a time stamp returned and is in T&Z format, ex: “2018-10-09T10:10:00Z”.
2) I have parsed the date and saved in a variable(date1) using “Regular Expression Extractor”.
3) In the successive request I would need to use the parsed time but this time I want to use it in millisecond format for the next request.
4) Here is my sample snippet looks like in “BSF PreProcessor”,
Here “date1” is a variable to which the value is parsed and extracted using Regular Expression Extractor.
Code snippet,
var time1 = vars.get(date1);
var timem1 = new Date(time1);
var timem1 = timem1.getTime();
vars.put("timem1",timem1);
But the above code didn’t help.
Can anyone please help me here?
Thanks in advance.

This is the format Instant can directly parse:
java.time.Instant.parse('2018-10-09T10:10:00Z').toEpochMilli()
// => 1539079800000

Please check the below;-
String b1 = "2018-10-09T10:10:00Z";
time=Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", b1)
// get epoch milis
epoch_milis = time.getTime()
log.info("Current date in the specified format:>>>>>>>>>>>> " + epoch_milis);
Please check if this helps.
Also, it is recommended to use JSR223 instead of beanshell due to performance.

Related

Reformatting date values when using them as URL parameters in a PowerQuery API request

I have two dates in my Excel table with the following format: "dd-mm-yyyy". These dates need to be sent as URL query parameters to an API endpoint for getting some data using PowerQuery. However, the API endpoint does not accept dates in that format. Therefore, I need to convert them to the format "mm-dd-yyyy" instead for it to work.
For getting the values from my table, I use the following code:
let GetNamedRange=(NamedRange) =>
let
name = Excel.CurrentWorkbook(){[Name=NamedRange]}[Content],
value = name{0}[Column1]
in
value
in
GetNamedRange
This function, called "GetValue", is then called when inserting URL query parameters in my GET request:
Csv.Document(Web.Contents("my.api/leave/leavecsv", [Query = [periodStart = GetValue("periodStart"), periodEnd = GetValue("periodEnd"), department = GetValue("department")]]),[Delimiter=";", Columns=14, Encoding=1252, QuoteStyle=QuoteStyle.None])
Currently the cells for my dates are in Text format. I tried using Date.FromText(...) to format the dates, but I get an error saying the datetime format is invalid.
https://learn.microsoft.com/en-us/powerquery-m/date-fromtext
How can I propertly format my date values before inserting them as URL query parameters using PowerQuery?
Ensure your dates are real dates and set to type date. then you can use the Date.ToText function:
let
theDate = #date(2022,12,7),
output = Date.ToText(theDate,"MM-dd-yyyy")
in
output
If, for some reason, you must maintain your dates as text strings (I'd like to know why, if that's the case), you can convert them first to a "real" date, and then create the string:
let
theDate = "13-12-2022",
output = Date.ToText(Date.FromText(theDate, "en-150"),"MM-dd-yyyy")
in
output
Make sure you pass in a culture and format. i.e.
Date.FromText([Column1], [Format="dd-MM-yyyy", Culture="en-UK"])

Nodejs change time at date type after getting from Mongodb

I wanted to change time at date type which returning from mongodb with custom time like below
"2021-05-26T00:00:00.000Z"
to
"2021-05-26T10:20:00.000Z"
I wanted to change time from a variable at the date, so my technique was split this date with "T" then get time part and change it with custom time
let splitedTime = timev[0].validFrom.toString().split()[0];
let customTime = "10:20:00.000Z";
let finalTime = splitedTime + customTime;
but this split not working this giving me date like this "Wed May 26 2021 06:00:00 GM". Can you please help me for this?
Working with Date
Whilst I understand your logic of converting it to a string and then using string methods to convert it to your desired output, I believe a simpler approach is to use the Date object
function dateAdd(original, hours, minutes) {
const date = new Date(original);
date.setHours(original.getHours() + hours);
date.setMinutes(original.getMinutes() + minutes);
return date.toISOString();
}
When original = "2021-05-26T00:00:00.000Z" then the return value is "2021-05-26T10:20:00.000Z".
If you want a fixed time:
const date = new Date('2021-05-26T00:00:00.000Z');
date.setUTCHours(10);
date.setUTCMinutes(20);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
// a cleaner approach:
date.setUTCHours(10, 20, 0); // hoursValue, minutesValue, secondsValue
console.log(date.toISOString());
Which produces the following:
"2021-05-26T10:20:00.000Z"
Another Solution
Your actual problem is being caused by the fact you call toString which returns a date string in the format of "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)" so when you're splitting by "T", that's way down at the end. toISOString will return the correct format.
Explanation
As you can see above, we avoid using string methods and use the methods that exist on Date. This approach is safer as you avoid issues with the difference between toISOString and toString. You may also find moment useful if you're using dynamic methods of changing dates regularly.
Note
In all honesty, I'm not entirely sure I understand the why behind what you're doing, so if I'm wrong please correct me so I can update my answer to be more relevant for you.
Learn More
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

How to convert date to hours from now using moment js

I have the following data 2020-07-06T02:46:57.883+00:00. How do I convert it using moment js to 5 hours ago. for example. I used the following code but it doesnot work.
moment('2020-07-06T02:46:57.883+00:00.', "YYYYMMDD").fromNow();
but it is not working correctly
It seems like you are trying to achieve two outcomes through single command.
Ideally, if you want to first format the date and then use the fromNow() method, I would suggest the following way:
const date = moment('2020-07-06T02:46:57.883+00:00');
const formatedDate = date.format('YYYYMMDD');
const time = date.fromNow();
OR You can chain these operations
const time = moment('2020-07-06T02:46:57.883+00:00').fromNow();
Note: format() method returns a string, fromNow() cannot be chained to format method's response.

Set field value

I have userevent script that I need to add a + 1 value to a field on edit.
This is what I have so far:
nlapiSubmitField('custbody1', + '1');
I am receiving an error, invalid expression. Please assist if you can.
Thanks
+ '1' is not valid JavaScript syntax.
You will need to retrieve the current value from custbody1 (presumably with a lookup), parse it as a Number, add 1 to the result, then that result is what you will pass to nlapiSubmitField.
You will need to store the returned value of nlapiGetFieldValue in a variable before using nlapiSetFieldValue. Something like:
var x = nlapiGetFieldValue('field1');
nlapiSetFieldValue('field1', parseInt(x) +1);

Moment.js failed to valid time when passing date

I'm reading values from an old csv file that I have. This file has a lot of wrong data, it should has only time values, but sometimes it has date values on it.
I'm trying to valid time with moment.js, but when I run this code:
const moment = require('moment');
console.log(moment('19/07/9130','hh:mm').isValid());
It returns true, I'm doing something wrong??
1.
First of all, you are giving wrong format in moment function, you are passing data in DD/MM/YYYY format and defining format hh:mm. You should pass correct format.
2.
19/07/9130 is valid date which have year 9130 and it's absolutely valid future year. If you want to restrict maximum year, you can use isBefore method
var mydate = moment('19/07/9130', 'DD/MM/YYYY');
var isValid = mydate.isValid() && mydate.isBefore('2050-01-01');
Reading more the Moment.js docs, I find a third parameter that make Moment use strict parsing.
const moment = require('moment');
console.log(moment('19/07/9130','hh:mm',true).isValid());
This works fine!

Resources