Date using only Javascript - javascript

Edit: Previously only HTML was there but in new update even Javascript works but purely Javascript ... I can't add HTML there...
I am working on a WordPress site, I need to display an image dynamically in a site. Image source will be like:
site.com/files/year/month/date/img1.jpg
But in my theme I can only input either HTML or JS there... generally if it is a static site then we can only use that image but my image gets updated daily... i.e.
today it will be site.com/files/2015/4/13/img1.jpg
tomorrow it will be site.com/files/2015/4/14/img1.jpg
How can I achieve this?

var image = document.getElementById('yourImagesId');
var date = new Date();
var url = 'site.com/files/' + date.getFullYear() + '/' + (date.getMonth() +1) + '/' + date.getDate() + '/img1.jpg';
image.setAttribute('src',url);

HTML is Hyper-Text Markup Language. It isn't "code" in the sense that it doesn't have any logic or power. It exists to literally markup your copy; that is, it's primary use is to define the layout of the content of your page.
To do anything dynamic, you need an actual coding language that can perform actual logic functions, so you'll have to utilize JavaScript (or PHP, as you're in Wordpress).
I'd be lying if I said I had experience in Wordpress's environment, but I'd imagine that googling "wordpress dynamic image path plugin" would possibly yield some tangible results.

Not sure what do you mean only with HTML, as for Javascript:
var currDate = new Date();
var url = "site.com/files/" + currDate.getFullYear() + "/"
+ (currDate.getMonth() + 1) + "/" + currDate.getDate()
+ "/" + imageName + "." + imageFormat;
getMonth will get the month as a number from 0 to 11 so you add 1 if needed to have it as 1 ~ 12.
JSFiddle Example
Be aware that JS takes the date and time from the client.

Related

Node.js console.log() in txt file with date and hour

I want to write into a txt file all the logs of my nodeJs program.
I'm using node script-file.js >log-file.txt, but it overwrites the same txt file every time it runs.
Is there a parameter to add so that the title of the generated log-file.txt includes the date and time like this : log-file-18-06-2010-11-57 (dd-mm-yyyy-hh-mm)?
Thanks to a friend of mine, I was able to answer my question.
As the solution node script.js > log.txt 2> errors.txt generates only one file of logs and one file of errors. Putting >> instead of > permit to append the logs and errors, but the notion of time is lost.
The SOLUTION is to simply do node script.js >> log-error.txt 2>&1 and it generates one file, where both logs and errors are transcribed into this file.
You have to console.log the date at the top of the script and it makes a reliable log-errors file.
To add the date (it will help beginners) just write this :
var today = new Date(); console.log(today.getDate() + '-' + today.getMonth() + '-' + today.getFullYear() + ' ' + today.getHours() + ':' + today.getMinutes() + ':' + today.getSeconds() + '.' + today.getMilliseconds())
How about using package? I think this is very simple way.
Here's the awesome log file package

Adobe Analytics DTM custom script prop not setting

I am trying to show the last time I made a published change and the current library version
I created a data element Global - Example:
return "DTM:" + _satellite.publishDate.split(" ")[0] + "|" + "Adobe:" + s.version;
Then I set a prop to my %Global - Example%.
It doesn't work. So I tried to debug in the console. When console.log("DTM:" + _satellite.publishDate.split(" ")[0] + "|" + "Adobe:" + s.version); it works in the console and I get the the last publish date and the current version of the library. However, it won't work for some reason in dtm.
Using a Data Element in this case will have an impact on timing.
If you add your code to the Adobe Analytics Custom Code section of a rule or AA Global Code you should be able to set your prop just fine.
s.prop1 = _satellite.publishDate.split(" ")[0] + "|" + "Adobe:" + s.version);
Hope this helps.

Convert Jquery date to custom timezone by timezone id

I have to display datetime value on my real time page and i have done following jquery function for that.
function DisplayTimer() {
var x = new Date();
$('#<%=lblTimer.ClientID %>').html(x.toString());
setTimeout('DisplayTimer()', 5000);
}
now i have timezonid value in my session object how can i convert above date value to custom timezone using timezonid session value and also want set datetime format as per culture of user's browser through this jquery function. I have solution in server side code so using [webmethod] i can do that it will make separate request for that every 5 second so i would like to do that without server side interaction. please help me if anyone done this type of logic.
Thanks in adavance.
Change your code with:
var ClientDatetime = x.getMonth() + "/" + x.getDate() + "/" + x.getYear() + " "
+ x.getHours() + ":" + x.getMinutes() + ":" + x.getSeconds();
take one hidden variable hdnClientDateTime with runate = server and set value as following
hdnClientDateTime.value = ClientDatetime;
Now, Pass hdnClientDateTime.value variable in your server side Datetime format function and assign the value into label like this:
$('#<%=lblTimer.ClientID %>').html(Result);

Select current date on calendar with javascript and html

I created a simple calendar with html and now want the current date to highlight automatically with javascript. I know of a few ways to do this but I am looking for the most simple.
It really depends on how your calendar works. You can get the client's current date with the following JavaScript:
var currentDate = new Date();
Once you have that, you'll have to use the built in date functions to get the current date element and probably add a class to it that will style it as highlighted.
UPDATE
Assuming you have your li elements with id="dayElement_x" where x is the day number, and your class for highlighting your day is currentDay, an example JavaScript call would be:
document.getElementById('dayElement_' + (new Date()).getDate()).className += ' currentDay';
UPDATE 2
I just thought of a solution where you can do this without having a bunch of ids. Here is the JavaScript:
document.getElementById('calendarContainer').
getElementsByTagName('li')[(new Date()).getDate() - 1].className += ' currentDate';

Why I get some numbers added to a String variable in Javascript?

I'm trying to concatenate some String variables in Javascript in order to make it contain a file path which has to be calculated to load some flash charts.
If I alert the variable it shows properly but when i invoke the method to load the chart I get an error cause the path it receives isn't the one I see on the alert and thus the one it should be. It's the same string but with some numbers added at the end preceded by an interrogation mark, like '?1297931086408' for example.
Therefore the path is the right path plus this substring which is 'unloadable'.
Thanks in advance for your help!
The problem is not on your javascript. It's on your server.
For a server, this path:
/path/to/my_file.csv
Should be equivalent to this other path:
/path/to/my_file.csv?1425432456
Inside a url, the part to the left of the question mark references the resource being requested. The part to the right are parameters.
That "random number" at the end is a parameter that prevents caching. If it wasn't there, most browsers will say "hey, I already know what is on /path/to/my_file.csv, I don't need to get it again" and will use a cached version of the data. Chances are that flashmovie.reloadData is adding that parameter itself.
Make sure that the file get downloaded if you type its address directly into the browser url bar. Then, try adding a question mark and some random numbers. If that doesn't work, then your server is being overzealous on its url validation.
The code is pretty simple:
function setDataModified(businessUnit){
var path = "https://....";//Complete path
var csvPath;
var xmlPath = path + "Monthly%20Charts/" + businessUnit + "/";
var csvPath = "";
csvPath = path + "Monthly%20Charts/";
//Take data from the form
var selectedMonth = document.getElementById("monthForm").value;
var selectedYear = document.getElementById("yearForm").value;
//Generate csv's path
csvpath += selectedYear + "/" + selectedMonth + "/Monthly_" + businessUnit + "standardChart__" + selectedMonth + "_" + selectedYear + ".csv";
//setData call
var flashMovie = document.getElementById("amcolumn1");
alert(csvPath);
flashMovie.reloadData(csvPath);//csvPath contains the path and also some numbers like '?129..'
}
When I try to load the new chart i get and error message pointing out the error and the wrong path, i.e. with the added numbers at the end.
Thanks!

Categories