Filesystem saves all filename with the same time - javascript

I have a javascript program with filesystem (saves user answer,etc) The file name I have implemented data and time however if the user repeats the program, and after that ends it. All the file will be saved with the same time
Tried putting it into a string. After the file system does it job, i clears the string. Which is suppose to work? because it will check for new local time.
function filesystem() {
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate(); //gets local machine year,date,time.
var time = today.getHours() + "Hour." + today.getMinutes() + " Min." + today.getSeconds() + " Seconds";
var dateTime = parseFloat("Date " + date + ' ' + "Time " + time);
var dateTimestr= "";
dateTimestr += (dateTime);
fs.writeFile("CA2/Quizresults/" + fullname + " " + categorystorage + " " + dateTimestr + ".txt", summarystring + "Your score is " + point + "/5.", (err) => {
if (err) throw err;
})
console.log('Saved!'); //when saved successfully. it will prompt the user.
point = 0; //Sets point as 0. so the point does not bring forward to the next user.
lifelinecounter = 0; //Sets lifelinecounter as 0. so the lifelinecounter does not bring forward to the next user.
summarystring = "";
dateTimestr =""; //Sets summarystring as empty. so the summarystring does not bring forward to the next user.
}
All the files saves with the same time in the file name

var today = new Date();
Please add the above as the first statement of the function. You have assigned today outside the function, and it's value is not going to change according to the running time. If you want current date, you will have to call new Date() again and assign it to your today variable.

So you bound the today variable to a new Date()
var today = new Date() // right?
now when filesystem function runs, it's looking at that variable, and that variable holds the same value forever from the moment when you initialize it.
So you need to reassign value at the beginning of the function like this:
function filesystem() {
today = new Date()
var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate(); //gets local machine year,date,time.
var time = today.getHours() + "Hour." + today.getMinutes() + " Min." + today.getSeconds() + " Seconds";
var dateTime = parseFloat("Date " + date + ' ' + "Time " + time);
var dateTimestr= "";
dateTimestr += (dateTime);
fs.writeFile("CA2/Quizresults/" + fullname + " " + categorystorage + " " + dateTimestr + ".txt", summarystring + "Your score is " + point + "/5.", (err) => {
if (err) throw err;
})
console.log('Saved!'); //when saved successfully. it will prompt the user.
point = 0; //Sets point as 0. so the point does not bring forward to the next user.
lifelinecounter = 0; //Sets lifelinecounter as 0. so the lifelinecounter does not bring forward to the next user.
summarystring = "";
dateTimestr =""; //Sets summarystring as empty. so the summarystring does not bring forward to the next user.
}

Related

Load JS function inside a div

I have a simple span and I want to show full date from Javascript inside this span. I'm not getting how to do it.
HTML (The date would be in place of the "..."):
<h3>Data Atual: </h3><span id="date" onload="newDate()">...</span>
Javascript:
function newDate() {
var dateBox = document.getElementById('date');
dateBox.innerHTML = '';
var date = new Date();
var newDate = date.getDay + ', ' + date.getDate + ' de ' + date.getMonth + ', ' + date.getFullYear + '.';
dateBox.innerHTML += newDate;
}
Thanks in advance
The load event doesn't fire on static HTML elements, only elements that load their data asynchronously from an external URL.
Put the call in the body's onload event.
<body onload="newDate()">
you can use this code to show the complete day in your span text:
document.getElementById("date").innerHTML = createNewDate();
function createNewDate() {
const date = new Date();
const newDate = date.getDay + ', ' + date.getDate + ' de ' + date.getMonth + ', ' + date.getFullYear + '.';
return newDate;
}
do not need to load it on start. Actually you are using get dates wrongly. This following code will solve it your problem. Put it before </body>
<script>
var date = new Date();
var newDate = date.getDay() + ', ' + date.getDate() + ' de ' + date.getMonth() + ', ' + date.getFullYear() + '.';
document.getElementById("date").innerHTML = newDate;
</script>

Google-app-script: Date formating issue in a javascript email body using a cell row[] in Spreadsheet

I tried to modify a script to my own needs but I get an error "TypeError: Function getDate not found in the object . (line 13, file "Code")".
In fact, I want to compare today's date to the date contained in row[7] and send a reminder to some people for each line of the spreadsheet containing Projects...
Here is my actual code:
function sendEmails(step) {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process, Start at second row because the first row contains the data labels
var numRows = sheet.getLastRow(); // Number of rows to process -> all rows which are not empty
var totalRows = numRows - startRow +1; // total rows to process is numRows on which we remove start row +1
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, totalRows, 20) // range of columns to process
// Fetch values for each row in the Range
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2] + ", " + row [3] + ", " + row [4]; // email addresses for email distribution
var messagePVEnd = "The PV of project " + "'"+ row[0] +"'" + " is ending the " + row[7].getDate() + "/" + (row[7].getMonth+1) + "/" + row[7].getFullYear() + " , please push " + row[1] + " to get the reports."; // Email content for PV End
var messagePVMidStatus = "The PV of project " + "'" + row[0] + "'" + " will be at 500h the " + row[6].getDate() + "/" + (row[6].getMonth()+1) + "/" + row[6].getFullYear() + " , please push " + row[1] + " to get the intermediate status."; // Email content for PV after 500h
var messagePVOut = "The PV of project " + "'"+ row[0] +"'" + " is supposed to be finished since " + row[7].getDate() + "/" + (row[7].getMonth()+1) + "/" + row[7].getFullYear() + " , please push " + row[1] + " to get the reports and confirm that all reports are received in PV follow up google sheet."; // Email content for PV Out
var subjectPVEnd = row [0] + " -- Reminder for PV ending to get report from " + row [1]; // Email subject for PV end
var subjectPVMidStatus = row [0] + " -- Reminder to get PV intermediate status after 500h from " + row [1]; // Email subject for PV after 500h
var subjectPVOut = row [0] + " -- Reminder, PV should be finished with all reports received from " + row [1]; // Email subject for PV Out
if (row[8]==5) { // if date of PV status after 500h is equal to 5 days
MailApp.sendEmail(emailAddress, subjectPVMidStatus, messagePVMidStatus)
}
else if (row[8]==1) { // if date of PV status after 500h is equal to 1 day
MailApp.sendEmail(emailAddress, subjectPVMidStatus, messagePVMidStatus)
}
else if (row[9]==5) { // if PV end date is equal to 5 days
MailApp.sendEmail(emailAddress, subjectPVEnd, messagePVEnd)
}
else if (row[9]==1) { // if PV end date is equal to 1 day
MailApp.sendEmail(emailAddress, subjectPVEnd, messagePVEnd)
}
else if (row[10]!="yes") {
for (var j=1; j<=9; j++) {
if (row[9]==-(j*10)) { // if PV end date is out of date by multiple of 10 days up to 100 days (except if report are received)
MailApp.sendEmail(emailAddress, subjectPVOut, messagePVOut)
}
}
}
}
}
The emails are sent properly but I get problems with the date format in the message and I cannot figure out what I did wrong.
Any support would be welcome!
Thanks in advance,
Edit 17/08:
Here is a picture of the corresponding spreadsheet.
enter image description here
Please note that row[7] this is cell value but not the object. So to compare with the current date you need var now = new Date(); and then compare it with the row[7]
It seems like the data returned from spreadsheet in row[7] is not a date type. In order to check manually, double click on the spreadsheet cell and see if a calendar pops up. If not, then the data returned from the sheet is not of date object type.
To do in code, first check if the row[7] is instanceof Date. If yes, then process as is, otherwise, convert the date string returned from that cell to a date object and then process further.
Part of modified code will look like this
var emailAddress = row[2] + ", " + row [3] + ", " + row [4]; // email addresses for email distribution
var dateStr = '';
if(row[7] instanceof Date){
dateStr = row[6].getDate() + "/" + (row[6].getMonth()+1) + "/" + row[6].getFullYear();
}
else {
dateStr = row[7];
}
// var messagePVEnd = "The PV of project " + "'"+ row[0] +"'" + " is ending the " + dateStr + " , please push " + row[1] + " to get the reports."; // Email content for PV End
var messagePVMidStatus = "The PV of project " + "'" + row[0] + "'" + " will be at 500h the " + dateStr + " , please push " + row[1] + " to get the intermediate status."; // Email content for PV after 500h
var messagePVOut = "The PV of project " + "'"+ row[0] +"'" + " is supposed to be finished since " + dateStr + " , please push " + row[1] + " to get the reports and confirm that all reports are received in PV follow up google sheet."; // Email content for PV Out
var subjectPVEnd = row [0] + " -- Reminder for PV ending to get report from " + row [1]; // Email subject for PV end

Jquery How to check today time greater then use given time?

How to check time conditionin Jquery
var startTime="20:02:55"; // or 12:34
var endTime ="21:02:55" // or 1:34
var dt = new Date();
var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
if(time >startTime || time < endTiime){
$("#a").html("show Box");
}else{
$("#a").html("Expire BOx");
}
How to check 12 hour and 24 hour condition also?
is it correct? i need am, pm format check please can anyone help me?
Here is some code. I am appending to show both behaviour.
Here is DEMO
test("20:02:55", "21:02:55");
test("13:02:55", "15:02:55");
function test(start_time, end_time) {
var dt = new Date();
//convert both time into timestamp
var stt = new Date((dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear() + " " + start_time);
stt = stt.getTime();
var endt = new Date((dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear() + " " + end_time);
endt = endt.getTime();
var time = dt.getTime();
if (time > stt && time < endt) {
$("#a").append("<br> show Box ");
} else {
$("#a").append("<br> Expire BOx ");
}
}
Try this.
I took the logic to print 'Show Box' if the current time is in between the start and end time. else viceversa.
var startTime="20:02:55"; // or 12:34
var endTime ="21:02:55" // or 1:34
var dt = new Date();
var st = new Date('00','00','00',startTime.split(':')[0],startTime.split(':')[1],startTime.split(':')[2]);
var et = new Date('00','00','00',endTime.split(':')[0],endTime.split(':')[1],endTime.split(':')[2]);
if(dt.getTime() >st.getTime() && dt.getTime() < et.getTime()){
alert("show Box");
}else{
alert("Expire BOx");
}

I have javascript to get new time and date

I have a JavaScript code to get new time. But when i reload the page the time doesn't change instead it says 1/10/2014 10:57, how can i add a function for the time and date to detect and change
Below is the code I tried but its not working.
<script type="text/javascript">
(function () {
// using current UTC time from server as ref, display local time in div id="now"
var now = new Date();
now.setTime(1389322677492);
var nowstr = ""
+ (now.getMonth() + 1) + "/"
+ now.getDate() + "/"
+ ((now.getYear() < 1000) ? now.getYear() + 1900 : now.getYear()) + " "
+ now.getHours() + ":"
+ ((now.getMinutes() < 10) ? '0' + now.getMinutes() : now.getMinutes());
var el = document.getElementById("now");
el.appendChild(document.createTextNode(nowstr));
})();
</script>
Thanks for helping me.
Thats because you are explicitly setting it to 1389322677492 everytime. All you need to do is remove the line:
now.setTime(1389322677492);

OpenTextFile gives error Automation server can't create Object

I created this method to create a text file as log for a page that has different buttons that call functions with parameters from different textfields:
function WriteToFile(data) {
var currentdate = new Date();
var datetime = "Time: " + currentdate.getDate() + "/" + (currentdate.getMonth()+1) + "/" + currentdate.getFullYear() + " # " + currentdate.getHours() + ":" + currentdate.getMinutes() + ":" + currentdate.getSeconds();
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.OpenTextFile("C:\\logs\\log.txt", 8);
a.WriteLine(datetime);
a.WriteLine(data + "\n");
a.Close();
}
And it works perfectly fine when I'm using the C: drive. Unfonrtunately, this is to be used in production in a Z: drive so other people can use the page as well. When I copy it to the Z: drive and change this line:
var a = fso.OpenTextFile("C:\\logs\\log.txt", 8);
to the following:
var a = fso.OpenTextFile("Z:\\logs\\log.txt", 8);
it gives me an error saying:
Automation server can't create object
I am using IE8. Any ideas on what I'm doing wrong and how I can fix this?

Categories

Resources