Read the time frame in the file line by line and assign to a variable in nodejs - node.js

I have a file which has values
Start : Wed Dec 18 2019 09:55:15 GMT+0530 (IST)
End : Wed Dec 18 2019 10:11:23 GMT+0530 (IST)
Duration : 00:16:07.278
So I need to get the values coming after
Start :
End :
Duration :
and assign to 3 variables.
Please let me know how we can try that in nodejs

you can parse the date as is with new Date(date string from your file)...
so just split the file into single lines and then remove the starting Start : etc...
as for the Duration, you can split on ":" and then turn the hours and minutes into seconds adding all together in the end...
var st = `Start : Wed Dec 18 2019 09:55:15 GMT+0530 (IST)
End : Wed Dec 18 2019 10:11:23 GMT+0530 (IST)
Duration : 00:16:07.278 `
var lines = st.split("\n")
var start = new Date(lines[0].split("Start :")[1])
var end = new Date(lines[1].split("End :")[1])
var a_dur = lines[2].replace("Duration :", "").split(":")
var dur = Number(a_dur[0]) * 60 * 60 + Number(a_dur[1]) * 60 + Number(a_dur[2])
e_start.innerHTML = start
e_end.innerHTML = end
e_duration.innerHTML = dur
Start:
<div id="e_start"></div><br/> End:
<div id="e_end"></div><br/> Duration:
<div id="e_duration"></div><br/>

Related

Using Jscript i want to change 2022-10-20T14:54:29.255+0800 to 13/10/2022 2:54 PM

i tried using this method :
newDateTime = new Date('2022-10-20T14:54:29.255+0800');
but it return me : "Thu Oct 20 2022 14:54:29 GMT+0800 (Singapore Standard Time)"

SOUP UI Groovy || For loop is looping 50 times where the condition is using this number anywhere

Here I am just taking value(integer) from Properties file and using the same in for loop.
Note : If I use direct number instead of "getTestCasePropertyValue" value it work as expected. Not getting how loop is looping it 50 times.
Groovy script:
def getTestCasePropertyValue = testRunner.testCase.getPropertyValue( "NumOfPayments" )
log.info(getTestCasePropertyValue )
for(i=0; i<=getTestCasePropertyValue; i++)
{
log.info("Test Print"+i)
}
Output:
Fri Mar 06 12:58:47 IST 2020:INFO:2
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print0
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print1
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print2
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print3
...
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print50
Your value from the properties is a String. You will detect problems like this easier, if you use .inspect() to log things.
Also the character '2' is 50 as integer, which then the for loop conditions casts this too.
def getTestCasePropertyValue = "2"
println(getTestCasePropertyValue.inspect())
// → '2'
println(getTestCasePropertyValue as char as int)
// → 50
So best explicitly cast to a number using e.g. .toLong() on the string:
println(getTestCasePropertyValue.toLong().inspect())
// → 2

Remove timestamp and url from string python

I have a string from which I have to remove the timestamp and punctuation. And I have to remove all the digits also but responseCode value
has to be kept as is for example 400 in this case. And wherever 400 comes, it should not be removed. And I want to remove all the url's
and file name ending with tar.gz.
mystr="sun aug 19 13:02:09 2018 I_am.98189: hello please connect to the local host:8080
sun aug 19 13:02:10 2018 hey.94289: hello not able to find the file
sun aug 19 13:02:10 2018 I_am.94289: Base url for file_transfer is: abc/vd/filename.tar.gz
mon aug 19 13:02:10 2018 how_94289: $var1={
'responseCode' = '400',
'responseDate' = 'Sun, 19 Aug 2018 13:02:08 ET',
'responseContent' = 'ABC' }
mon aug 20 13:02:10 2018 hello!94289: Error performing action, failed with error code [400]
"
Expected result:
"I_am hello please connect to the local host
hello not able to find the file
Base url for file_transfer
var1
responseCode = 400
responseDate
responseContent = ABC
Error performing action, failed with error code 400
"
My Solution to remove punctuation:
punctuations = '''!=()-[]{};:'"\,<>.?##$%^&*_~'''
no_punct = ""
for char in mystr:
if char not in punctuations:
no_punct = no_punct + char
# display the unpunctuated string
print(no_punct)
Maybe:
patterns = [r"\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\s*", #sun aug 19 13:02:10 2018
r"\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} \w{2}\s*", #Sun, 19 Aug 2018 13:02:08 ET
r":\s*([\da-zA_Z]+\/)+([a-zA-Z0-9\.]+)", #URL
r"([a-zA-Z_!]+)[\.!_]\d+:\s*", #word[._!]number:>=0space
r":\d+",
"[/':,${}\[\]]" #punctuations
]
s = mystr
for p in patterns:
s = re.sub(p,'', s)
s = s.strip()
print(s)
Output:
hello please connect to the local host
hello not able to find the file
Base url for file_transfer is
var1=
responseCode = 400
responseDate =
responseContent = ABC
Error performing action failed with error code 400

TimeCategory add minutes using vars

How do i add minutes to currentDate.
I might add more than 1440 minutes..
def AddMinutes = 1445
currentDate = new Date();
println currentDate
use( TimeCategory ) {
NewCurrentDate = currentDate + AddMinutes.minutes // fails
NewCurrentDate = currentDate + 1445.minutes // works
}
println currentDate
Tue Feb 23 15:09:13 CET 2016
Wed Feb 24 15:14:13 CET 2016
Works for me... Can't see your problem apart from you're not printing out the newCurrentDate (PS: Lower case letters for variable names, otherwise Groovy can get confused, and think you're on about classes -- but that's not the issue here)
import groovy.time.*
def addMinutes = 1445
currentDate = new Date()
use( TimeCategory ) {
newCurrentDate = currentDate + addMinutes.minutes
}
println currentDate //Tue Feb 23 14:45:52 GMT 2016
println newCurrentDate //Wed Feb 24 14:50:52 GMT 2016
I found out that I needed to make sure that the addMinutes was an int. So I added this before in my script:
addMinutes = addMinutes.toInteger()
and now it works.

convert a Date string without timezone in nodejs

I want to convert a string with just the date part, into a Date object in nodejs.
If I do this:
console.log('2010-10-05 ||', new Date('2010-10-05'));
console.log('2010-10-05 00:00:00 ||', new Date('2010-10-05 00:00:00'));
I obtain this in console:
2010-10-05 || Mon Oct 04 2010 19:00:00 GMT-0500 (CDT)
2010-10-05 00:00:00 || Tue Oct 05 2010 00:00:00 GMT-0500 (CDT)
I don't want '2010-10-05' to be converted into '2010-10-04' because of my timezone.
My timezone is -0500 GMT.
How can I create a date by just providing the Date part without the gap ?
Use zeros for hour, minute, second etc.
var date = '2010-10-05',
arr = date.split('-'),
obj = new Date(arr[0], arr[1], arr[2], 0, 0, 0, 0);
Date.UTC uses universal time instead of the local time, if that's what you need
Date.UTC(arr[0], arr[1], arr[2], 0, 0, 0, 0)
console.log('2010-10-05 ||', new Date('2010-10-05' + ' UTC'))
console.log('2010-10-05 00:00:00 ||', new Date('2010-10-05 00:00:00' + ' UTC'))

Resources