Instance of local time variable in js - javascript

I am using a build variable which gives current date and time
let build = new Date().toLocaleDateString() + "_" + new Date().toLocaleTimeString();
For an automation run, anywhere I call this variable, the value changes.
But I need for an automation run, the build value should be same. Again i trigger a second run, at that time build should fetch new value, how is that possible ?

let build = new Date().toLocaleDateString() + "_" + new Date().toLocaleTimeString();
localStorage.build = build;
console.log(localStorage.build);
You can calculate value once and store somewhere like localStorage and read it from there.
PS : localStorage will not work in RUN CODE SNIPPET due to
cross-origin issues.

Everytime the script runs, it assigns a new time stamp to the build variable.
You need to create the time stamp in some outside script, which only runs once and assign the value to a variable, which you can use accross your application.
For eg:
(function createTimestamp(){
build = new Date().toLocaleDateString() + "_" + new Date().toLocaleTimeString();
})()
Here build is global variable which you can use across the script.

Related

How to store output to a variable using Execute Javascript new Date().toLocaleDateString() in Robot Framework?

I am using Robot Framework and I want to capture the date that is generated by this method:
Execute Javascript new Date().toLocaleDateString()
If I do something like the below the trace log file reflects ${result} = none. I expect the output to be ${result} = current date
${result} = Execute JavaScript Date().toLocaleDateString()
Missing the return function.
${result}= Execute JavaScript ldate = Date().toLocaleString(); return ldate;
EDIT: Changed code to return an object.

JS objects for Timer on Webpage

What are the objects in JavaScript that we use for running time on webpage?
I tried using getElementId() method and could not do it.
If you want to get the current date and time,
var now = new Date()
var deadline = new Date(now.getFullYear, now.getMonth, now.getDate, now.getHours, now.getMinutes + 15);
Then you can fill an element with the deadline variable.
See here
https://blog.smalldo.gs/2013/12/create-simple-countdown/

Get time using cookies javascript

How to get Time with Cookies javascript When I close the tab or Page?
i'm try use window.onunload but time can not be stored in cookies..
in this code i'm push my cookies to be array
setCookie("time",time,1)
window.onunload =function (){
var set = getCookie('time');
var arr = [];
var push = arr.push(set);
console.log (arr);
return arr;
}
In JavaScript, there is a class called "Date" that let's you get the current time.
var d = new Date(); creates a new Date object and assigns it to the variable d
Now, you have the date and time - you can do whatever you like.
For example: document.getElementById('foo').innerHTML = d.toString() finds the element with the ID of "foo" and sets it's contents to the date and time.
Keep in mind that this date and time display will not constantly update itself. You will have to do that with some timed recursion.
There are many more things you can do with dates. Remember, Google is your friend - look up some things about the Date object.

Javascript Date can't be stored

I'm having a strange issue with javascript : when stocking a new Date().getTime() in a certain variable, it goes to 0.
console.log(new Date().getTime()); // Shows the timestamp
p.channels[p.chann].startTime = new Date().getTime();
console.log(p.channels[p.chann].startTime); // Shows 0
The variable p.channels[p.chann].startTime totally exists (and would otherwise show an error anyway...)
Does anyone know where this problem could come from ? Oo
The problem was that, my variable being an Audio element, I could create the startTime value in it, but it was set to 0 and couldn't be set to anything else.
I have a created a JS Fiddle to show that this works.
p = {};
p.channels = [];
p.chann = 0;
p.channels[p.chann] = {};
p.channels[p.chann].startTime = new Date().getTime();
document.write(p.channels[p.chann].startTime);
The likely reasons for this failing would be that p.chann has been changed between setting and extracting the value, or p.channels[p.chann] hasn't been set as an object (you can test this by commenting out the line where p.channels[p.chann] = {};.
Since this code works, the only issue could be the existance of p.channels[p.chann].startTime while you're retrieving the value, or maybe in your real code you're reading a different value (maybe p.chann changes meanwhile because you're in a loop or you created a closure... it's hard to tell)
var p = {
chann : 0,
channels : [
{ startTime : 0 }
]
};
p.channels[p.chann].startTime = new Date().getTime();
console.log(p.channels[p.chann].startTime); // Shows the value

javascript/python time processing fails in chrome

I am writing a timer web app,which records start time and stop time.It uses javascript,jquery1.4.2 for the front end and python for backend code.When a start button is clicked ,start time is saved in a javascript variable.when the button is clicked again, stop time is saved in another variable.These values are passed as hidden parameters to the python code which gets the start,stop values from django's request parameter.
I expect the start/stop parameters values to be in the following format
"07:16:03 PM"
so that it can be parsed using '%I:%M:%S %p'format string.
I am getting this correctly in mozilla firefox.But when I use chrome,I only get
"19:16:03"
This causes value error when I try to parse it with the above format string.
import time
...
def process_input(request,...):
start_time=request.POST[u'timerstarted']
...
fmtstr='%I:%M:%S %p'
start_time_list = list(time.strptime(start_time,fmtstr)[3:6])
I tried putting alert('start time set as'+start_time) in javascript to find what values are set in the page's hiddenfields
With firefox ,I got
start time set as08:03:09 PM
stop time set as08:03:43 PM
but with chrome
start time set as20:04:21
stop time set as20:04:32
My knowledge of javascript,jquery is minimal.Why is the script behaving differently in these two browsers? Below is the javascript snippet
$(document).ready(function(){
var changeBtnStatus=function(){
var timebtnvalue=$('#timebtn').attr("value");
if (timebtnvalue =="start"){
...
var start_date=new Date();
var str_time=start_date.toLocaleTimeString();
var timerstartedfield =$('#timerstarted');
timerstartedfield.attr("value",str_time);
alert('start time set as'+str_time);
}
else if (timebtnvalue=="stop"){
...
var stop_date=new Date();
var stp_time=stop_date.toLocaleTimeString();
var timerstoppedfield =$('#timerstopped');
timerstoppedfield.attr("value",stp_time);
alert('stop time set as'+stp_time);
}
};
var timerBtnClicked=function(){
...
changeBtnStatus();
};
$('#timebtn').click(timerBtnClicked);
...
}
);
You don't want the string of the time in locale, using the toString method you can provide your own format, or use toUTCString().
toLocaleTimeString is especially meant to display the time as the user is used to, you want it in a set format.
So instead of start_date.toLocaleTimeString(), you want to use start_date.toUTCString().
Why format the time in JavaScript and parse in Python, and even submit yourself to the confusion of different locales?
Try using Date.getTime insteam:
start_time = (new Date).getTime();
stop_time = (new Date).getTime();
This gets you the time in milliseconds since the epoch, which should always be stable.

Categories