Problems calculating date - javascript

Can someone help me determine why this code is not working?
<script type="text/javascript">
const date = new Date();
const day = date.getUTCDate();
const month = date.getMonth(); //January is 0!
const md = month + "." + day
if (md < 3.03) {
var theDate = new Date();
var award_year1 = date.getFullYear();
var award_year2 = date.getFullYear() + 1;
}
else if (md > 3.03) {
var theDate = new Date();
var award_year1 = date.getFullYear() + 1;
var award_year2 = date.getFullYear() + 2;
}
console.log(award_year1);
console.log(award_year2);
</script>
Today is June 7, so it should return award_year1 = 2019 and award_year2 = 2020.

Just a suggestion to make your code a little more declarative/ easier to read. You can compare 2 date objects in the way you would expect to.
const date = new Date();
let offset = 0;
const threshold = new Date();
threshold.setMonth(3);
threshold.setDate(3);
if (Date.now() > threshold) { //might be a bit easier to reason about
offset = 1;
}
var theDate = new Date();
var award_year1 = date.getFullYear() + offset;
var award_year2 = date.getFullYear() + 1 + offset;
console.log(award_year1);
console.log(award_year2);

Related

calculate startdate and enddate of week, yields a strange result when 1st of new month

I use the code below to calculate the start and enddate of the week based on the current date.
As i live in sweden, the first day of the week is on a monday.
if the date is 31th of august 2021 the result is 20210830-20210905
But now, when the date becomes 1st of september 2021 the result is 20210830-20210805?
How is it even possible?
I am running this locally on a firebase cloud emulator and in the firebase cloud function live, the result are the same.
function currentWeeklyId() {
const dateNow = new Date();
// First day is the day of the month
const first = dateNow.getDate() - dateNow.getDay() + 1;
const last = first + 6;
const fDay = new Date(dateNow.setDate(first));
const lDay = new Date(dateNow.setDate(last));
var firstMonth = fDay.getMonth() + 1;
if (firstMonth < 10) firstMonth = '0' + firstMonth;
var firstDay = fDay.getDate();
if (firstDay < 10) firstDay = '0' + firstDay;
var lastMonth = lDay.getMonth() + 1;
if (lastMonth < 10) lastMonth = "0" + lastMonth;
var lastDay = lDay.getDate();
if (lastDay < 10) lastDay = "0" + lastDay;
const docId = dateNow.getFullYear() + firstMonth + firstDay + "-" + dateNow.getFullYear() + lastMonth + lastDay;
return docId;
}
The problem lies on this line
const lDay = new Date(dateNow.setDate(last));
You are changing the date, but not the month. dateNow points to August month.
The lDay should reference new fDay while setting date.
const lDay = new Date(dateNow.setDate(fDay.getDate()+6))
function currentWeeklyId() {
const dateNow = new Date();
// First day is the day of the month
const first = dateNow.getDate() - dateNow.getDay() + 1;
const fDay = new Date(dateNow.setDate(first));
const lDay = new Date(dateNow.setDate(fDay.getDate() + 6))
var firstMonth = fDay.getMonth() + 1;
if (firstMonth < 10) firstMonth = '0' + firstMonth;
var firstDay = fDay.getDate();
if (firstDay < 10) firstDay = '0' + firstDay;
var lastMonth = lDay.getMonth() + 1;
if (lastMonth < 10) lastMonth = "0" + lastMonth;
var lastDay = lDay.getDate();
if (lastDay < 10) lastDay = "0" + lastDay;
const docId = dateNow.getFullYear() + firstMonth + firstDay + "-" + dateNow.getFullYear() + lastMonth + lastDay;
return docId;
}
Seems for your calculation of lastDay you need to add in the original date- as of right now you’re just setting the day of the month rather than adding.
lDay = dateNow.setDate(dateNow.getDate() + last)

JavaScript is returning NAN for a string '021519' on html

I have a formatted date as a string '021519' using javascript which return NAN on display in html.
Note I have a xslt using the javascript.
var newDate = '';
var formatedDate = new Date(date);
var year = formatedDate.getFullYear().toString();
var month = (1 + formatedDate.getMonth()).toString();
if(parseInt(month) < 10)
{
month = "0" + month;
}
var day = formatedDate.getDate().toString();
if(dateFormat == '1')
{
newDate = month + day + year.substr(2,4);
}
else
{
newDate = month + day + year;
}
var newLeftStart3 = parseInt(startPosition) - 1;
var newLeftEnd3 = newLeftStart + newDate.length;
var newRightStart3 = parseInt(endPosition) - newDate.length;
var newRightEnd3 = newRightStart + newDate.length;
if(alignment == '1')
{
addendaSpace = addendaSpace.substr(0, newLeftStart3) + newDate + addendaSpace.substr(newLeftEnd3);
}
if(alignment == '2')
{
addendaSpace = addendaSpace.substr(0, newRightStart3) + newDate + addendaSpace.substr(newRightEnd3);
}
newDate is displaying as NaN i hope this code helps.
I usually format dates in javascript this way:
var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1; //Months are zero based
var year = d.getFullYear();
console.log(date + "-" + month + "-" + year);

Month/Day comparison in Javascript

I'm not an expert in JavaScript, but I've been tasked with migrating a very large website that was coded into PHP into a new CMS that does not allow server-side languages. I am therefore spending my days converting many PHP date-calcs to JavaScript.
Something very simple in PHP:
<?php
if (date('md') < 816) {$award_year = date('Y');}
if (date('md') > 815) {$award_year = date('Y') + 1;}
/*echo "year ".$award_year;*/
?>
This allows the year an application is due to automatically change to next year after August 15.
I've been trying to recreate this effect with Javascript and here is what I have come up with:
var today = new Date();
var mm = today.getMonth()+1; //January is 0!
var dd = today.getday();
if (mm < 10) {
mm = '0'+mm
}
currDate = mmdd;
var currDate = new Date();
var appDate = new Date("0816");
if (currDate < appDate){
var printDate = theDate.getFullYear();
}
else if (currDate >= appDate){
var printDate = theDate.getFullYear()+1;
}
I know that I am missing something, because the var currDate cannot just = mmdd and then be compared to another date. Can someone help me with the next step here ? I'm trying to actually learn JavaScript as I go rather than just blindly fix issues.
This will look similar to your PHP code:
const date = new Date();
const day = date.getUTCDate();
const month = date.getMonth();
const md = month + "" + day;
if (md < 816) {
var award_year = date.getFullYear();
}
else if (md > 815) {
var award_year = date.getFullYear() + 1;
}
console.log(award_year);
Instead of combining strings and comparing two dates you could check the current day and month to determine which year to return.
For example,
function getAwardYear() {
const today = new Date()
const AUGUST = 7
if (today.getMonth() >= AUGUST && today.getDate() > 15) {
return today.getFullYear() + 1
}
return today.getFullYear()
}
// Today: 6th Jun 2018
getAwardYear() // 2018
// Today: 16th Aug 2018
getAwardYear() // 2019
I know there is plenty of things to improvise the below code... I am leaving it for you to improvise...
var today = new Date();
var mm = today.getMonth()+1; //January is 0!
var dd = today.getDay();
if (mm < 10) {
mm = '0'+mm
}
var yyyy="2018"
var currDate = new Date(yyyy+"-"+mm+"-"+dd);
var appDate = new Date("2018-01-16");
alert("appDate"+appDate);
alert("currDate"+currDate);
if (currDate < appDate){
var printDate = currDate.getFullYear();
alert(printDate);
}
else if (currDate >= appDate){
var printDate = appDate.getFullYear()+1;
alert(printDate);
}

javascript print weekend date

Hello Guys i want to show the week ending date in javascript or through some jquery calendar plugin with below functionality
It looks something like this:
Week Ending: < show weekending date in dd/MMM/YYYY format >
and then if we click on these left and right arrows it should show next weekend in same format.
kindly please suggest me if there is a plugin with similar functionality or if we can achieve this with simple javascript
Use this to get the first and last day of week.
var curr = new Date; // get current date
var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
var last = first + 6; // last day is the first day + 6
var firstday = new Date(curr.setDate(first)).toUTCString();
var lastday = new Date(curr.setDate(last)).toUTCString();
console.log("Week Start : "+firstday);
console.log("Weekend : "+lastday);
var date = new Date(lastday);
var formattedDate = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear();
console.log(formattedDate);
You can change lastday format manually using getDay(), getMonth() and getFullYear()
Try This .
var current = new Date(); // get current date
var weekstart = current.getDate() - current.getDay() +1;
var weekend = weekstart + 6; // end day is the first day + 6
var monday = new Date(current.setDate(weekstart));
var sunday = new Date(current.setDate(weekend));
<html>
<head>
<script>
function myFunction(data) {
if(data == "down")
change = change - 7;
else if(data == "up")
change = change + 7;
var someDate = new Date();
var day = someDate.getDay();
var numberOfDaysToAdd = 0;
if(day == 1) { numberOfDaysToAdd = 5; }
else if(day == 2) { numberOfDaysToAdd = 4; }
else if(day == 3) { numberOfDaysToAdd = 3; }
else if(day == 4) { numberOfDaysToAdd = 2; }
else if(day == 5) { numberOfDaysToAdd = 1; }
someDate.setDate(someDate.getDate() + numberOfDaysToAdd + change);
var dd1 = someDate.getDate();
var mm1 = someDate.getMonth() + 1;
var yy1 = someDate.getFullYear();
someDate.setDate(someDate.getDate() + 1);
var dd2 = someDate.getDate();
var mm2 = someDate.getMonth() + 1;
var yy2 = someDate.getFullYear();
var someFormattedDate = dd1 + '/'+ mm1 + '/'+ yy1 + '\n' + '&' +
dd2 + '/'+ mm2 + '/'+ yy2;
document.getElementById("showWeekends").innerHTML = someFormattedDate;
}
</script>
</head>
<body>
<button onclick="myFunction('down')"><</button>
<span id="showWeekends"></span>
<button onclick="myFunction('up')">></button>
</body>
<script>
var change = 0;
myFunction("none");
</script>
</html>

Javascript Date Function not working

I am trying to display the text 'Oct 09, 2012'. Instead it is not running the function and is displaying a lot of unnessecary date text. Does anyone know what I am doing wrong?
You can play with my jsfiddle... http://jsfiddle.net/UP3fd/
Here is the code...
var myDate = new Date();
convertDate(myDate);
myDate.setFullYear(2012, 9, 9);
document.write(myDate);
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = "0" + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct"," Nov","Dec"];
var currentMonth = months[month];
return (currentMonth + " " + day + ", " + year);
}
You are calling your function before you set your date, and you are not saving/outputting the return value anywhere.
var myDate = new Date();
myDate.setFullYear(2012, 9, 9);
document.write( convertDate(myDate) );
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = "0" + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct"," Nov","Dec"];
var currentMonth = months[month];
return (currentMonth + " " + day + ", " + year);
}
​
var strDate = addZero(d.getDate()) + "/" + addZero((d.getMonth() + 1))+
"/" +d.getFullYear();
alert("strDate :"+strDate)
return strDate;
}
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
This is the correct code:
var myDate = new Date();
myDate.setFullYear(2012, 9, 9);
myDate = convertDate(myDate);
document.write(myDate);
[...]
Here's the corrected code, this should return you what you are expecting.
var myDate = new Date();
myDate.setFullYear(2012, 9, 9);
var newDate = convertDate(myDate);
document.write(newDate);
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = "0" + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct"," Nov","Dec"];
var currentMonth = months[month];
return (currentMonth + " " + day + ", " + year);
}

Categories