Display Current date with additional 30 days of calculation - JS - javascript

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>

Related

How can change the format of Date in javascript?

I have a date like 2019-02-01. how can I change it to February 2019?
You can just do this:
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date("2019/02/01");
const dateString = `${monthNames[d.getMonth()]} ${d.getFullYear()}`;
console.log(dateString);
This will return the month in English, no matter the system's language.
Note that you should use new Date("2019/02/01") (with slashes) to specify a date in your timezone. If you use new Date("2019-02-01"), then it will assume UTC time, and weird stuff will happen (see this answer for more info on this).
const d = new Date("2019/02/01");
const m = d.toLocaleString('default', { month: 'long' });
console.log(m, d.getFullYear());
/* you can use the following code to convert date to the required format. */
var todaysDate = new Date();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var formatted_date = months[todaysDate.getMonth()] + " " + todaysDate.getDate() + " " + todaysDate.getFullYear();

update date.js and remove document.write

I would like help to update this date.js code to remove the document.write statements as it is now obsolete
if (Date.parse(document.lastModified) != 0) {
var modiDate = new Date(document.lastModified);
var monthName = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
document.write(monthName[modiDate.getMonth()] + " ");
document.write(modiDate.getDate() + ", " + modiDate.getFullYear());
}
What do I put in place of document.write(...... please
the javascript code produces a (File) Updated on September 12, 2019 at the bottom of the web page via this code snipit
<p>Updated <script type="text/javascript" src="/public/jsscripts/date.js"> </script>
Thanks Mark
You can use insertAdjacentHTML, which is the closest equivalent replacement of document.write together with currentScript to get the HTML element of the script, that's being evaluated.
<p>Updated
<script>
var modiDate = new Date(document.lastModified);
var monthName = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
var text = monthName[modiDate.getMonth()] + " ";
text += modiDate.getDate() + ", " + modiDate.getFullYear();
document.currentScript.insertAdjacentHTML('afterend', text);
</script>
</p>
Something like this maybe.
It'll update all elements with the class documentLastModified.
<p>Updated <span class="documentLastModified" /></p>
<script>
var modiDate = new Date(document.lastModified);
[].slice.call(document.querySelectorAll('.documentLastModified')).forEach((el) => {
el.innerHTML = modiDate.toDateString();
});
</script>
Use getElementById and toLocaleDateString
// date.js
document.getElementById('updated-date').textContent = new Date().toLocaleDateString('en-US', { /* new Date(document.lastModified)... */
month: 'long',
day: 'numeric',
year: 'numeric'
})
<p>Updated <span id="updated-date"></span></p>
<!-- end of body
<script type="text/javascript" src="/public/jsscripts/date.js">-->

Format array elements from one form to another -JS

I have an array of strings that are actually dates in the form of 201001 ,201002. I would like to convert those array items to something like 2010 January, 2010 February. Is there any way to do this.
var Array = ["201005", "201006", "201007", "201008", "201009", "201010", "201011", "201012", "201101", "201102", "201103", "201104", "201106", "201107", "201108", "201109", "201110", "201111", "201112", "201201", "201202", "201203", "201204", "201205", "201206", "201207", "201208", "201209", "201210", "201211", "201212", "201301", "201302", "201303", "201304", "201305", "201306", "201307"];
I'm looking for an array like :
var expected = ["2010 january", "2010 February" etc]
You could do this:
const monthMap = {
"01": "January",
"02": "February",
"03": "March",
"04": "April",
"05": "May",
"06": "June",
"07": "July",
"08": "August",
"09": "September",
"10": "October",
"11": "November",
"12": "December"
};
xAxisArray = xAxisArray.map(axis => {
const year = axis.substring(0, 4);
const month = axis.substring(4, 6);
return `${year} ${monthMap[month]}`;
});
Or you could use moment, but that might be overkill.
You can try my code
function formatDate(date) {
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
var monthIndex = date.getMonth();
var year = date.getFullYear();
return year + ' ' + monthNames[monthIndex];
}
var result = xAxisArray.map(item => {
var strDate = item.slice(0, 4) + '-' + item.slice(4, 6) + '-01'
return formatDate(new Date(strDate))
})
console.log(result)
This is a demo: https://codepen.io/phuongnm153/pen/xxKgKYp
You can try the following code:
var xAxisArray = ["201005", "201006", "201007", "201008", "201009", "201010", "201011", "201012", "201101", "201102", "201103", "201104", "201106", "201107", "201108", "201109", "201110", "201111", "201112", "201201", "201202", "201203", "201204", "201205", "201206", "201207", "201208", "201209", "201210", "201211", "201212", "201301", "201302", "201303", "201304", "201305", "201306", "201307"];
//Array to link month number to name
const monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
//Result array
var formattedArray = [];
//Loop through the old array and push the formatted date to the new array
xAxisArray.forEach(function(element) {
var formatted = element.substr(0,4) + '-' + element.substr(4); //Add the dash between year and month
var date = new Date(formatted); //Create date object
var year = date.getFullYear(); //Get the year
var month = date.getMonth(); //Get the month
formattedArray.push(year + ' ' + monthNames[month]);
});
console.log(formattedArray);
First, create a date from the string. Then convert that date to the desired format and last add the new formatted string to a new array.
If you want to have the full name of the month, and not just the first three letter, you need to have them somewhere, for example:
const MONTHS = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
Then you can simply have:
const format = (value) => {
// assuming the format is 4 digits followed by 2 digits
const [, year, month] = value.match(/(\d{4})(\d{2})/);
return `${year} ${MONTHS[month - 1]}`
}
Then you can map your array such as:
const expected = xAxisArray.map(format);
Hope it helps!
You can get your answer by following approach .
xAxisArray.forEach(function(value , index){
var last2 = value.slice(-2);
const date = new Date(value.slice(4), last2); // 2009-11-10
const month = date.toLocaleString('default', { month: 'long' });
xAxisArray[index] = value.substring(0,4) +" "+ month;
});
Your problem will get solve .
You can use moment library for this:
const result = xAxisArray.map(item => {
const year = item.substring(0,4)
const month = item.substring(4,2)
return moment(`01/${month}/${year}`).format('YYYY MMMM')
})
console.log(result)
By using moment.js libary you can convert , use below expression convert it
moment("your value").format("YYYY MMM");
ex:moment(201005).format("YYYY MMM");

Enter date into HTML onload Javascript

I am trying to enter the current date when the page loads. I am able to store it to a variable, I am just having trouble entering that variable onto the page.
HTML:
<span id='test'>x</span>
Javascript:
window.onload = function() {
var month = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var currentMonth = month[mm];
var yyyy = today.getFullYear();
today = currentMonth + ' ' + dd + ', ' + yyyy;
document.getElementById('test').innertext() = today;
}
JSFIDDLE
Change this:
document.getElementById('test').innertext() = today;
for this:
document.getElementById('test').innerHTML = today;
Use this code, it works
window.onload = function() {
var month = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var currentMonth = month[mm];
var yyyy = today.getFullYear();
today = currentMonth + ' ' + dd + ', ' + yyyy;
document.getElementById('test').innerHTML = today;
}
You have a problem at the following line of code because you treat the innerText property as if it was a function and you don't camel-case it (JavaScript is a case-sensitive language):
document.getElementById('test').innertext() = today;
So change it to:
document.getElementById('test').innerText = today;
Here is the working example: http://jsfiddle.net/ynevet/cNCf4/

Day not showing up in JS Date Script

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>

Categories

Resources