I have been trying to display the following "on this 4th day of January in the year 2022"
using javascript on an HTML page
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDay()
var dayString;
if (dayn == 1) {
dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
dayString = "</b><sup>th</sup>"
} else {
dayString = "</b><sup>nd</sup>"
}
var dateStr = "<b>" + dayn + dayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(dateStr);
but for some reason it is displaying today as the 2nd and not the 4th and
where I want to display ie. 4th March 2022 (today +89 days) it is failing to do so...
So the aim is to on the first line display:
"on this 4th day of January in the year 2022"
and on the next line
"no later than 3rd day of March 2022"
You have to use d.getDate() instead of d.getDay()
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDate();
var endDate = d.setDate(dayn + 89);
var daym = new Date(endDate).getDate();
var dayString;
if (dayn == 1) {
dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
dayString = "</b><sup>th</sup>"
} else {
dayString = "</b><sup>nd</sup>"
}
var endDayString;
if (daym == 1) {
endDayString = "</b><sup>st</sup>"
} else if (daym == 3) {
endDayString = "</b><sup>rd</sup>"
} else if (daym >= 4) {
endDayString = "</b><sup>th</sup>"
} else {
endDayString = "</b><sup>nd</sup>"
}
var endDateStr = "<b>" + daym + endDayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(endDateStr);
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDate();
var dayString;
if (dayn == 1) {
dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
dayString = "</b><sup>th</sup>"
} else {
dayString = "</b><sup>nd</sup>"
}
var dateStr = "<b>" + dayn + dayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(dateStr);
Related
I am trying to display current date by finding string and replacing with current date, its working fine, but additionally I want to display another date where if string has some comma seprated value then it will add to the current date and will display accodrdingly, So lets say if I add (,30) in string it will add 30 days additional in current date and display
var setCurrentDate = function() {
var disclaimerStr = $(".dynamic-date").html(),
currDateStr = "{currentdate}",
date = new Date(),
months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
currDate =
months[date.getMonth()] +
" " +
date.getDate() +
", " +
date.getFullYear(),
newDisclaimerStr;
if (disclaimerStr.indexOf(currDateStr) != -1) {
newDisclaimerStr = disclaimerStr.replace(currDateStr, currDate);
$(".dynamic-date").html(newDisclaimerStr);
}
};
setCurrentDate();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dynamic-date">
<b>Current Date</b> : {currentdate} <br><br>
<b>Extended Date</b> : {currentdate,30} <br><br>
</div>
Try this code :
var setCurrentDate = function() {
var disclaimerStr = $(".dynamic-date").html(),
currDateStr = "{currentdate}",
date = new Date(),
months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
currDate =
months[date.getMonth()] +
" " +
date.getDate() +
", " +
date.getFullYear(),
newDisclaimerStr;
if (disclaimerStr.indexOf(currDateStr) != -1) {
newDisclaimerStr = disclaimerStr.replace(currDateStr, currDate);
$(".dynamic-date").html(newDisclaimerStr);
}
var reg = new RegExp(/\{currentdate(,(\d+))\}/);
var currDateStr2 = '{currentdate,30}'; // you need to change here!
var days = parseInt(reg.exec(currDateStr2)[2], 10);
console.log(days) //30
var date2 = new Date();
date2.setDate(date2.getDate() + days);
console.log(date2) // "2019-04-27T09:13:00.789Z"
};
setCurrentDate();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dynamic-date">
<b>Current Date</b> : {currentdate} <br><br>
<b>Extended Date</b> : {currentdate,30} <br><br>
</div>
$("select").change(function() {
//var date = "2017-04-06"
//var date = "2017-01-06"
$(".appendhere").empty();
var date = $("option:selected",this).text();
var monthnumber = date.split("-")[1] - 1;
var yearnumber = date.split("-")[0];
var months = 12;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
for (var i = months - 1; i >= 0; i--) {
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>2017-04-06</option>
<option>2017-01-06</option>
</select>
<div class="appendhere"></div>
On change of select I want to append months with year based on the date of select.
I can add the month and year but the problem is the year for December is always 1 year ahead of the year.
I know it is small details but I cant point out where i did wrong.
Substitute "January" for "December" at conditional operator.
m_names[monthnumber] == "January" ? yearnumber++ : yearnumber + 0;
Reverse these two lines:
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
You're currently incrementing the month before testing whether the year should change.
$("select").change(function() {
//var date = "2017-04-06"
//var date = "2017-01-06"
$(".appendhere").empty();
var date = $("option:selected",this).text();
var monthnumber = date.split("-")[1] - 1;
var yearnumber = date.split("-")[0];
var months = 12;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
for (var i = months - 1; i >= 0; i--) {
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>2017-04-06</option>
<option>2017-01-06</option>
</select>
<div class="appendhere"></div>
Because you have increased monthnumber before check its name m_names[monthnumber] == "December" ?.
You should change the order of 2 below line of code:
From:
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
To:
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
$("select").change(function() {
//var date = "2017-04-06"
//var date = "2017-01-06"
$(".appendhere").empty();
var date = $("option:selected",this).text();
var monthnumber = date.split("-")[1] - 1;
var yearnumber = date.split("-")[0];
var months = 12;
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
for (var i = months - 1; i >= 0; i--) {
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
}
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option>2017-04-06</option>
<option>2017-01-06</option>
</select>
<div class="appendhere"></div>
reverse the last two line dude
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
https://jsfiddle.net/sa6onxgc/3/
Hope this will work.
for (var i = months -1; i >= 0; i--) {
monthnumber == 11 ? monthnumber = 0 : monthnumber++;
var div = $('<div class="row" style="margin-top:6px;"><div class="col-md-6"><label class="col-md-6 col-form-label">' + yearnumber + " " + m_names[monthnumber] + '</label></div>');
$(".appendhere").append(div)
m_names[monthnumber] == "December" ? yearnumber++ : yearnumber + 0;
}
I have been trying to get the months full name using the script (shown below) but couldn't able to get it.. seems like some thing is missing..
Code as follows:
var d=new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var timeString = d.getDate() + '-' + d.getFullYear()
+ ' ' + d.getHours() + ':' + d.getMinutes() + ':'
+ d.getSeconds();
document.getElementById("date").innerHTML = (monthNames[d.getMonth()]) + timeString;
<div>
<small><B><p id="date" name="date" class="date"></p></B></small>
</div>
Any Help is Appreciated..
var d=new Date();
Date.prototype.getMonthName = function() {
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
return monthNames[this.getMonth()];
}
var timeString = d.getDate() + '-' + d.getMonthName() + '-' + d.getFullYear()
+ ' ' + d.getHours() + ':' + d.getMinutes() + ':'
+ d.getSeconds();
document.getElementById("date").innerHTML =timeString;
Your code is OK
There is one simple issue when creating timeString variable. Instead monthNames you should invoke your method "getMonthName" instead just asking for variable you created inside getMonthName.
var timeString = d.getDate() + '-' + d.getMonthName() + '-' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
Here is a little fiddle that does what you want.
<div>
<p id="date" name="date" class="date">Date</p>
</div>
<script>
Date.prototype.getMonthName = function() {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return monthNames[this.getMonth()];
};
var d = new Date();
var monthName = d.getMonthName();
var timeString = d.getDate() + '-' + monthName + '-' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
document.getElementById("date").innerHTML = timeString;
</script>
Notice setting date and time in your paragraph is outside of prototype declaration. Also beware of return statement (code after it won't execute)!
I have to two dates from and to. I want to get all of the month names between these two dates.
Following is my code
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
}
above code works for following inputs
diff('September 2013', 'December 2013');
but it does not work for this
diff('September 2013', 'February 2014');
How can I make it work?
Mine is better: http://jsfiddle.net/kS73f/8/
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var arr = [];
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var fromYear = datFrom.getFullYear();
var toYear = datTo.getFullYear();
var diffYear = (12 * (toYear - fromYear)) + datTo.getMonth();
for (var i = datFrom.getMonth(); i <= diffYear; i++) {
arr.push(monthNames[i%12] + " " + Math.floor(fromYear+(i/12)));
}
return arr;
}
console.log(diff('September 2013', 'March 2014'));
You're going to have to do a more manual method than slice. Here's a starting point you can determine how to handle cases as mentioned in comments.
function diff(from, to) {
var result = [];
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
if(datFrom < datTo) {
var month = datFrom.getMonth();
var toMonth = datTo.getMonth() + 1 + ((datTo.getYear() - datFrom.getYear())*12); //toMonth adjusted for year
for(; month < toMonth; month++) { //Slice around the corner...
result.push(monthNames[month % 12]);
}
}
return result;
}
diff('September 2013', 'February 2014'); //=["September", "October", "November", "December", "January", "February"]
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr;
if(datFrom > datTo) {
return diff(to, from);
}
var fromYear = datFrom.getFullYear();
var toYear = datTo.getFullYear();
if(fromYear === toYear) {
return monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
} else {
var arr = addYear(monthNames.slice(datFrom.getMonth(), new Date('1 December ' + fromYear)), fromYear);
for(var i = 1; i < (toYear - fromYear); i++) {
arr = arr.concat(addYear(monthNames, fromYear + i));
}
return arr.concat(addYear(monthNames.slice(new Date('1 January ' + fromYear).getMonth(), datTo.getMonth() + 1), toYear));
}
}
function addYear(arr, year) {
var updatedArr = [];
for(var i = 0; i < arr.length; i++) {
updatedArr[i] = arr[i] + ' ' + year;
}
return updatedArr;
}
Than try console.log(diff('September 2013', 'February 2015')) to test it.
The following modifies the original function as little as possible, if that helps the OP from a comprehension standpoint.
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
if (!arr.length) {
arr = monthNames.slice(datFrom.getMonth(), 12);
arr = arr.concat(monthNames.slice(0, datTo.getMonth() + 1));
}
return arr;
}
console.log(diff('December 2013', 'February 2014')); //["September", "October", "November", "December", "January", "February"]
Lazy answer:
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var mFrom = new Date('1 ' + from).getMonth();
var mTo = new Date('1 ' + to).getMonth();
mTo = mTo < mFrom ? mTo + 12 : mTo;
return monthNames.slice(mFrom, mTo + 1);
}
alert(diff('September 2013', 'December 2013'));
alert(diff('September 2013', 'February 2014'));
Trying to show the date on the webpage as "Wednesday, January 22, 2014" Instead I am getting a undefined, January 22, 2014.
What am I missing?
<p>Today's date is: <script type="text/javascript">
<!--
var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var months = new Array(
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December");
var currentTime = new Date();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var year = currentTime.getFullYear();
document.write(days[day] + ", " + months[month] + " " + day + ", " + year);
//-->
</script>
not that you've asked but what about using moment.js
Nevermind. Should have tried one more thing:
<p>Today's date is: <script type="text/javascript">
<!--
var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var months = new Array(
"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December");
var currentTime = new Date();
var today = currentTime.getDay();
var month = currentTime.getMonth();
var day = currentTime.getDate();
var year = currentTime.getFullYear();
document.write(days[today] + ", " + months[month] + " " + day + ", " + year);
//-->
</script>