I'm having trouble updating the current year using js from an external file. The year in the html doesn't update to display the current year. Here's my html code;
<p>
Copyright © <span id="year">year</span>. All rights reseverved.
</p>
<script src="./js/shared.js"></script>
And here's my js code;
const year = document.querySelector('#year');
function date() {
year.innerHTML = new Date()
};
What am I doing wrong here, and what's the right way to achieve my goal?
Just try to add function invocation:
document.addEventListener('DOMContentLoaded', function(){
const year = document.querySelector('#year');
function date() {
year.innerHTML = new Date().getFullYear();
};
date();
}
Related
the main idea is to get the number of the week between two dates (from a period of start date and end date)!
Something like that: if the period is 01-05-2020 to 31-05-2020 and in the data picker I chouse 08-05-2020 result will be 2, the second week.
can someone help with that can't figure out by my self,
thank you!
here is JS date picker code line with setting up period 01-05-2020 to 31-05-2020, how to echo out from this js code number of week for future php usage or input value?
js
<!-- js -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js' type='text/javascript'></script>
<!-- Datepicker -->
<link href='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css' rel='stylesheet' type='text/css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js' type='text/javascript'></script>
<script type="text/javascript">
$(document).ready(function(){
$('#datepicker').datepicker({
format: "dd/mm/yyyy",
startDate: new Date('2020-5-1'),
endDate: new Date('2020-5-31')
});
});
</script>
html
<div class="form-group">
<label for="formGroupExampleInput">Date of sale</label>
<input type="text" name="dos" value="<?php echo $doc; ?>" class="form-control" id='datepicker' id="formGroupExampleInput">
</div>
Accordingly to your main idea, you can use date-fns library to get the difference between date in weeks.
// import date-fns library
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/2.0.0-alpha0/date_fns.min.js"></script>
Then you can:
let diffCalendarWeeks = dateFns.differenceInCalendarWeeks(new Date(2020,4,31), new Date(2020,4,1));
let diffWeeks = dateFns.differenceInWeeks(new Date(2020,4,31), new Date(2020,4,1))
console.log(diffCalendarWeeks); //5
console.log(diffWeeks); // 4
See date-fns documentation
https://date-fns.org/v2.0.0-alpha.1/docs/differenceInCalendarWeeks
If you want to know the week number within specified month you can see the difference between the selected date and first date.
let selectedDate = new Date(2020, 4, 15);
let startOfMonth = dateFns.startOfMonth(new Date());
var selectedWeek = dateFns.differenceInWeeks(selectedDate, startOfMonth) + 1;
console.log(selectedWeek);
Or you can try this answer if fits you better
Getting current week of current month
Thanks, I found a solution to my problem.
Here it is, maybe someone needed..
<script>
function diff_weeks(dt2, dt1)
{
var diff =(dt2.getTime() - dt1.getTime()) / 1000;
diff /= (60 * 60 * 24 * 7);
return Math.abs(Math.round(diff));
}
dt1 = new Date(2014,10,2);
dt2 = new Date(2014,10,11);
console.log(diff_weeks(dt1, dt2));
dt1 = new Date("June 13, 2014 08:11:00");
dt2 = new Date("October 19, 2014 11:13:00");
console.log(diff_weeks(dt1, dt2));
<script>
lnk.
https://www.w3resource.com/javascript-exercises/javascript-date-exercise-47.php
I wanted to make a bookmark that uses today's date in the URL; in other words, when the bookmark is launched, the end of the URL would vary each day. So today's would end in .../2017/1/31 and tomorrow's would end in .../2017/2/1.
I thought it might be easiest to just make a barebones HTML page that includes an inline JavaScript to get current year, month, and date and append it to the main URL (which never changes). Does this make sense? Is there an easier way to accomplish this?
I'm okay with HTML elements, but kind of clueless about JavaScript; I literally copied a snippet from another stackoverflow answer that sounded decent and put it into my head tags as you can see below, and tried to adapt my URL into the ahref link:
<HTML>
<head>
<script>var d=new Date();</script>
</head>
<body>
<a href="http://wol.org?t="+d.getTime()>Continue</a>
</body>
</HTML>
The following will run without need for clicking any buttons:
<HTML>
<head>
<script>
Date.prototype.yyyymmdd = function() { //returns YYYY/MM/DD
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [this.getFullYear(),
(mm>9 ? '' : '0') + mm,
(dd>9 ? '' : '0') + dd
].join('/');
};
var date = new Date();
window.location.href = "your.url.com/" + date.yyyymmdd();
</script>
</head>
<body>
</body>
</HTML>
Date function from this answer: https://stackoverflow.com/a/3067896/3803371
Note I usually don't condone modification of native prototypes, but I'm feeling lazy today.
You cannot use javascript expression outside script tag. So you cannot call d.getTime like this. Instead of you can do this:
<a id="c" href="">Continue</a>
<script>
(function() { // wait for window load
var d=new Date();
var a = document.getElementById("c");
a.href = "http://wol.org?t="+d.getTime();
})();
</script>
There's a couple problems with your code. First, you're mixing HTML and JavaScript. JavaScript can only go between the <script> tags. Also, the script needs to go below your link you want to modify.
If you want to get the date in the form year/month/day you'll have to do some modification to the date string you get back from your Date object. What I do below is basically get the date string and split it by / into an array. I know the first index is the month, second is the day, and third gives me the year. I store each of those into a variable to use and rearrange later.
I then had to locate the <a> element using getElementById() and then I changed the href value using my date variables.
var dateString = new Date().toLocaleDateString();
var dateArray = dateString.split('/');
var month = dateArray[0];
var day = dateArray[1];
var year = dateArray[2];
var dateOrder = year + "/" + month + "/" + day;
console.log(dateOrder);
var a = document.getElementById('link');
a.href += dateOrder;
<a id="link" href="http://wol.org?t=">Continue</a>
<script>
// Javascript from above goes here
</script>
Using Moment.js, I can't determine the way to show a date in a defined locale (eg. Fr). Any help is appreciated.
<script>
var NowMoment = moment().format("dddd, MMMM Do");
// display value of moment object in #displayMoment div
var eDisplayMoment = document.getElementById('displayMoment');
eDisplayMoment.innerHTML = NowMoment;
</script>
Add moment+locales.js or the locale/fr.js that you need...
moment.locale('fr');
var NowMoment = moment().format("dddd, MMMM Do");
// display value of moment object in #displayMoment div
var eDisplayMoment = document.getElementById('displayMoment');
eDisplayMoment.innerHTML = NowMoment;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment-with-locales.min.js"></script>
<div id="displayMoment"></div>
I believe you do:
<script>
var NowMoment = moment();
NowMoment.tz('Europe/Paris').format("dddd, MMMM Do");
// display value of moment object in #displayMoment div
var eDisplayMoment = document.getElementById('displayMoment');
eDisplayMoment.innerHTML = NowMoment;
</script>
I am not 100% sure it works as I am not able to test it. I got it from http://momentjs.com/timezone/
I am using javaScript that detects the current year, and then wish to inset this into the HTML. I have the following but it seems to be undefined.
Here is my code...
HTML
<p>this year is <span id="year"></span></p>
JavaScript
var date = document.write(new Date().getFullYear());
document.getElementById("year").innerHTML = date;
I can do this in jQuery but I am trying to do this in vanailla javaScript, learning at the same time.
Thanks in advance.
Make sure your element is read and ready to be manipulated:
Also fix your JS:
<body>
<p>this year is <span id="year"></span></p>
<!-- JS before /body -->
<script>
document.getElementById("year").innerHTML = new Date().getFullYear();
</script>
</body>
Note that if my computer clock is off, I'll see the wrong Year.
If you use PHP it's simple as:
<p>this year is <span id="year"><?php echo date("Y"); ?></span></p>
in short:
<?= date("Y") ?>
Would it not be simpler and more efficient to simply do this in-line?
<p>this year is <script>document.write(new Date().getFullYear());</script>.</p>
<script>
window.onload = function(){
var date = new Date().getFullYear();
document.getElementById("year").innerHTML = date;
}
</script>
<p>this year is <span id="year"></span></p>
var date = (new Date()).getFullYear();
document.getElementById("year").innerHTML = date;
The problem is the document.write call. Try this:
var date = new Date().getFullYear();
document.getElementById("year").innerHTML = date;
This Javascript should work:
var date = (new Date().getFullYear()).toString();
document.getElementById("year").innerHTML = date;
to show date dynamically
<%=DateTime.Now.Year %>
<p>©2016 School Apps. All Rights Reserved.</p>
To show 2016 dynamically we use that code
I recently upgraded to eclipse indigo from galileo. There has been some changes in the default formatter in my jsp pages.
<script type="text/javascript">
var DAY = '<%=Constants.SALES_DAY%>';
var WEEK = '<%=Constants.SALES_WEEK%>';
var MONTH = '<%=Constants.SALES_MONTH%>';
var YEAR = '<%=Constants.SALES_YEAR%>';
</script>
Ctrl-Shift-F on the file produces:
<script type="text/javascript">
var DAY = '<%=Constants.SALES_DAY%>';
var WEEK = '<%=Constants.SALES_WEEK%>';
var MONTH = '<%=Constants.SALES_MONTH%>';
var YEAR = '<%=Constants.SALES_YEAR%>
';
</script>
which breaks the page when deployed. Where and how can I modify the setting responsible for this behavior?
found this post which helped and got info here.
http://www.eclipse.org/forums/index.php/m/769392/#msg_769392
looks like an eclipse bug.