How to create .txt file based on date and time - javascript

I'm trying to create files based on current time and date inside a scheduler.
Here's what I've tried so far.
var logFileNameScheduler = schedule.scheduleJob('*/2 * * * *', function () {
let date_ob = new Date();
let date = ("0" + date_ob.getDate()).slice(-2);
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
let year = date_ob.getFullYear();
let hours = date_ob.getHours();
let minutes = date_ob.getMinutes();
var st = 'data';
logFileName = 'Error log report at : ' + date + "-" + month + "-" + year + " " + hours + ":" + minutes + '.txt';
fs.writeFile(logFilename, st, (err) => {
if (err) console.log(err);
console.log("Successfully Written to File.");
});
});
But it creates corrupted files. I can't open them.

Related

Why can't I print the current time alongside the if/else statement that uses the current time also?

I'm not sure why I can't print the message alongside the current time. The current time only prints when I comment out the if/else/else logic statement. I'm not sure why this is happening
let currentdate = new Date();
let tod = currentdate.getHours();
let dateTime =
currentdate.getDate() +
"/" +
(currentdate.getMonth() + 1) +
" " +
currentdate.getHours() +
":" +
currentdate.getMinutes();
window.onload = function() {
document.getElementById("time").innerHTML = dateTime;
};
if (tod < 11) {
message = "Good Morning.";
} else if (tod < 16) {
message = "Good Afternoon.";
} else {
message = "Good Evening.";
}
window.onload = function() {
document.getElementById("tod").innerHTML = message;
};

document.lastModified only displays current date?

I've been looking for a way to display the date the page last was updated.
Now I've been searching around, and everything points to the document.lastModified function, but however I've tried to fix it, it always shows the current date.
I've tried this example:
function lastModified() {
var modiDate = new Date(document.lastModified);
var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" + modiDate.getFullYear();
return showAs
}
function GetTime() {
var modiDate = new Date();
var Seconds
if (modiDate.getSeconds() < 10) {
Seconds = "0" + modiDate.getSeconds(); }
else {
Seconds = modiDate.getSeconds(); }
var modiDate = new Date();
var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
return CurTime }
document.write("Last updated on ");
document.write(lastModified() + " # " + GetTime());
document.write(" [D M Y 24 Hour Clock]"); document.write("");
Or a simple one like this:
<SCRIPT LANGUAGE="JavaScript">
var t = new Date(document.lastModified);
document.write("<I>Last Updated: "+document.lastModified+"</I><BR>");
document.write("<I>Last Updated: "+t+"</I><BR>");
</SCRIPT>
Is there any other way to do this?
.. Without taking a 3 years tech-class?
Press here to see the scripts live
Because you are modifying it currently. Check this out for example.
To make this work based on your requirement, checkout this link and this link
check this it will help u
Put this on the page at the bottom:
<script type="text/javascript" src="js_lus.js"></script>
Name the file whatever you want. Example: js_lus.js Make sure src=""
path is correct for all your pages.
function lastModified() {
var modiDate = new Date(document.lastModified);
var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" +
modiDate.getFullYear();
return showAs
}
function GetTime() {
var modiDate = new Date();
var Seconds
if (modiDate.getSeconds() < 10) {
Seconds = "0" + modiDate.getSeconds();
} else {
Seconds = modiDate.getSeconds();
}
var modiDate = new Date();
var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
return CurTime
}
document.write("Last updated on ")
document.write(lastModified() + " # " + GetTime());
document.write(" [D M Y 24 Hour Clock]")
document.write("");

JS: Formatting time and date display on website

I have a script that prints the current date and time in JavaScript, but when it prints time, it's missing one 0. Here is the code:
var currentdate = new Date();
var datetime = "0" + currentdate.getDate() + ".0"
+ (currentdate.getMonth()+1) + "."
+ currentdate.getFullYear() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes();
document.write(datetime);
It should print 04.03.2016 15:04 and prints 04.03.2016 15:4.
Two digit minutes print fine.
Any leads?
Try this
var formatDateDigit = function (i) {
return i <= 9 ? ("0" + i) : i;
};
var currentdate = new Date();
var datetime = formatDateDigit(currentdate.getDate()) + "."
+ formatDateDigit(currentdate.getMonth()+1) + "."
+ currentdate.getFullYear() + " "
+ formatDateDigit(currentdate.getHours()) + ":"
+ formatDateDigit(currentdate.getMinutes());
document.getElementById('my_output_here').innerHTML = datetime;
<div id="my_output_here"></div>

InDesign scripting error "ReferenceError: Object is invalid"

I'm trying to bring back to life and older script I had used that worked in the past. The script would download comics (that we have the rights to) using autohotkey and curl... Then in InDesign we would run the following from the javascript Scripts panel:
#targetengine "session"
var date, month, year, myDocument;
var curDate = new Date();
var myTemplatePath = "/c/Comic/ComicImport.indd";
var myComicsPath = "/c/Comic/Comics/";
var myTemplate = new File(myTemplatePath);
if (myTemplate.exists) {
try {
myDocument = app.open(myTemplate);
} catch (e) {
alert("Could not open template, exiting\n" + e);
exit();
}
var win = showDialog();
} else {
alert("Could not locate template at:\n" + myTemplatePath + "\nexiting");
}
function showDialog() {
var win = new Window('palette');
with(win){
win.Pnl = add('panel', undefined, 'Date / Month / Year');
win.Pnl.orientation = 'row';
with(win.Pnl) {
win.Pnl.day = add('edittext');
win.Pnl.day.text = curDate.getDate();
win.Pnl.day.preferredSize = [30,20];
win.Pnl.month = add('edittext');
win.Pnl.month.text = curDate.getMonth() + 1;
win.Pnl.month.preferredSize = [30,20];
win.Pnl.year = add('edittext');
win.Pnl.year.text = curDate.getFullYear();
win.Pnl.year.preferredSize = [50,20];
}
win.btnOk = add('button', undefined, 'Import Comic');
win.btnOk.onClick = setDate;
};
win.center();
win.show();
return win;
}
function setDate() {
date = win.Pnl.day.text;
month = win.Pnl.month.text;
year = win.Pnl.year.text;
// OK we close the window and do the import
//win.close();
importComics();
}
function importComics() {
try {
//set comic1 to "macintosh Hd:users:marshall:documents:comics:" & DYear & Dmonth & Dday & "pzjud-a.tif"
var comics = new Array();
// REPLACE with own filepaths, could be
//comics.push(new File("/c/comics/" + year + month + date + "pzjud- a.tif"));
comics.push(new File(myComicsPath + "comic1-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic2-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic3-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic4-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic5-" + year + "-" + month + "-" + date + ".tif"));
} catch (e) {
alert("Error assigning images for import, stopping script\n" + e);
exit();
}
for (i = 1; i <= comics.length; i++) {
// Script label of the rectangles/pageitems to place the graphics into
var myRect = myDocument.pageItems.item("comic" + i);
try {
myRect.place(comics[i-1]);
} catch (e) {
alert(e);
}
myRect.fit(FitOptions.CONTENT_TO_FRAME);
}
}
However as soon as I hit the Import Comic button, I get the "ReferenceError: Object is invalid" error. My directory structures look ok to me. Any ideas?
thanks!
Watch this line:
var myRect = myDocument.pageItems.item("comic" + i);
In newest ID version it is no longer calling "item.label" but "item.name"
(the one shown in Layer Panel)
If inside your doc target rectangles have "label == comic + i" you have to repeat/move this values as rectangle's name as well.
Otherwise - your code needs to create a loop through all pageItems and check particular item.label before placing image.

Format JavaScript Date as Hours:Minutes:Seconds

I have this code and I cannot get the second time to format properly:
setInterval(function() {
var local = new Date();
var localdatetime = local.getHours() + ":" + local.getMinutes() + ":" + local.getSeconds();
var remote = new Date();
var remotedatetime = remote.getHours() + ":" + remote.getMinutes() + ":" + remote.getSeconds();
var remoteoffset = remote.setHours(local.getHours() - 5);
$('#local-time').html(localdatetime);
$('#remote-time').html(remoteoffset);
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
My Time:
<div id="local-time"></div>
Their time:
<div id="remote-time"></div>
local-time is perfect and displays "hh:mm:ss"
remote-time just displays a list of random numbers.
How can I make remote-time "hh:mm:ss", too?
You're adjusting remote after getting its string representation, so that's doing you no good.
Then you're displaying the result of setHours() (milliseconds since January 1, 1970) rather than the string.
This is what I think you're aiming for:
setInterval(function() {
var local = new Date();
var localdatetime = local.getHours() + ":" + pad(local.getMinutes()) + ":" + pad(local.getSeconds());
var remote = new Date();
remote.setHours(local.getHours() - 5);
var remotedatetime = remote.getHours() + ":" + pad(remote.getMinutes()) + ":" + pad(remote.getSeconds());
$('#local-time').html(localdatetime);
$('#remote-time').html(remotedatetime);
}, 1000);
function pad(t) {
var st = "" + t;
while (st.length < 2)
st = "0" + st;
return st;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
My Time:
<div id="local-time"></div>
Their time:
<div id="remote-time"></div>
setInterval(function() {
var local = new Date();
var localdatetime = local.getHours() + ":" + local.getMinutes() + ":" + local.getSeconds();
var remote = new Date();
remote.setHours(local.getHours() - 5);
var remotedatetime = remote.getHours() + ":" + remote.getMinutes() + ":" + remote.getSeconds();
$('#local-time').html(localdatetime);
$('#remote-time').html(remotedatetime);
},1000);

Categories