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..
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>
I have a string like "2016-04-13". I need to change it to a date format like
"13 April, 2016". How can I do this in JavaScript?
What I've tried:
new Date(dateString);
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
The solution using String.replace function and predefined month names list:
var dt = new Date("2016-04-13".replace("-","/")),
monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var dateStr = dt.getDate() +" "+ monthNames[dt.getMonth()] +", "+ dt.getFullYear();
console.log(dateStr); // 13 April, 2016
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;
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/
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>