Day not showing up in JS Date Script - javascript

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>

Related

Display Current date with additional 30 days of calculation - JS

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>

Coverting DATETIME string to time and date in words Javascript or jQuery

i have a datetime parameter in a string from an JSON object
like this-
datetime:"2015-04-15 09.00.00"
my goal is to convert it to:
"15 April 09:00"
(2015 can be included as well but not necessary)
After i will append in in 2 different places, date and time.
Any suggestions? Can't seem to find any good info apart from getting a plugin.
You can parse the month like the answer here:
https://stackoverflow.com/a/1643468/2498251
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
so for your case you can make something like this:
var d = new Date("2015-04-15 09:00:00");
var day = d.getDay();
var month = monthNames[d.getMonth()];
var year = d.getYear();
var hour = d.getHours();
var min = d.getMinutes();
var fullDatetime = day + ' ' + month + ' ' + hour + ':' + min;

Displaying my date to a specific format

I am having a little difficulty with this.How can I display a date value to format 10 September 2014 using Javascript.
What I am doing at the moment is
var dateAdded = new Date(parseInt(user.DateAdded.substr(6)));
which gives me Wed Sep 10 2014
duplicate of How to format a JavaScript date
but Marko's accepted answer there for you:
Taken from http://www.webdevelopersnotes.com/tips/html/javascript_date_and_time.php3:
<script type="text/javascript">
<!--
var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
document.write(curr_date + "-" + m_names[curr_month]
+ "-" + curr_year);
//-->
</script>
You can edit the array to use Jan, Feb, Mar, etc..

How to format date like "July 14, 2014" in javascript/jQuery? [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
I'm getting date value from jQuery BeatPicker like "14/07/2014", I need to format this value as "July 14, 2014".
Is there any easy way to achieve this using javascript/jQuery/plugin?
Thanks
If you wanted to stick with pure Javascript
function formatDate(dateString){
var monthNames = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var dateArray = dateString.split("/");
var month = monthNames[dateArray[1]-1];
var day = dateArray[0];
var year = dateArray[2];
return month + " " + day + ", " + year;
}
var text = "14/07/2014";
var output = text.replace(/(\d\d)\/(\d\d)\/(\d\d\d\d)/, function($0, $1, $2, $3) {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return monthNames[parseInt($2, 10) - 1] + " " + $1 + ", " + $3;
});
Totally untested, and yes - the regex is messy (should use curly brackets however this reads more easily), but should work about right...

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/

Categories

Resources