I have 2 Date Objects. One is from and another contains to.
The date format used is "YYYY-MM-DD".
My problem is that I am using a charting tool and I have to map dates
on charting tool based on data.
So I need to divide this From and To Date based on data
if I have a selection from 2015-10-9 to 2015-12-23
I can break this down into n number of selection. Usually data length to be represented on x axis is 10 at max.
Yes, here's working code.
function test(){
var a=new Date(2015, 00, 01);
var b=new Date(2015, 05, 07);
ret=divideDates(a, b);
document.getElementById("results").innerHTML+="Start Date was: "+a.toString()+"<br/><hr />";
for(var key in ret){
document.getElementById("results").innerHTML+="<br />"+ret[key];
}
document.getElementById("results").innerHTML+="<br/><hr />End Date was: "+b.toString();
}
function divideDates(startDate, endDate){
var thediff=endDate-startDate;
var thetenth=thediff/10;
var resp = [];
for(i=1; i<=10; i++){
var toAdd=thetenth*i;
resp[i]=new Date(startDate.getTime()+toAdd);
}
return resp;
}
and here is a test HTML page i used (obv.ly call the js file "func.js"):
<html>
<head>
<script language="JavaScript" src="func.js"></script>
</head>
<body>
<input type="button" onclick="javascript:test();" value="testIT">
<div id="results">
</div>
</body>
</html>
Related
I'm using a CMS system that works alongside a backend that we must use. I'm trying to have it display a production lead time date such as www.customink.com. Current day +10 business days I'm using a HTML widget in this custom CMS, so I'm not sure if that is relevant. I'm struggling getting it to show up, and I really have no idea what I'm doing. I tried my best lol. Here's what I have thus far.
EDIT: Also, If I want it to only display M-F how do I go about that? I want it to read - Guaranteed by Fri, Oct 11.
<html>
<body>
<script>
document.getElementById("p1").innerHTML = newDate;
function addDays(theDate, days) {
return new Date(theDate.getTime() + days*24*60*60*1000);
}
var newDate = addDays(new Date(), 10);;
</script>
</body>
</html>
document.getElementById("p1").innerHTML = newDate;
In this statement, what you are doing is trying to find the DOM element with id p1 and then setting the value of newDate variable as its value. Therefore you should define an element with such id before your script block.
Then you are declaring and setting the value of the newDate after you have set the value for the above element. But you should do it before that.
See the fixed code below.
(I have used a <div> element as the p1 element. But you can use any other options such as <span>, <h1>, <h2>, etc. based on your requirement.)
<html>
<head>
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js'></script>
</head>
<body>
<div id='p1'></div>
<script>
var newDate = addDays(new Date(), 10);
document.getElementById("p1").innerHTML = formatDate(newDate);
function addDays(theDate, days) {
//return new Date(theDate.getTime() + days*24*60*60*1000);
return moment(theDate).add(days, 'd').toDate();
}
function formatDate(date) {
return moment(date).format('[Guaranteed by] ddd, MMM DD');
}
</script>
</body>
</html>
I need to add a date and time clock to a website. It should display the date and time in a very specific format, taking up two lines and using an intended timezone of GMT-4; for example:
Sat, Mar 23 2019
10:33:56 PM
This happens to be for a school project, but I have limited knowledge of Javascript. I've tried looking up some examples and have found some interesting stuff, but I'm unable to adapt those examples to generate the output in the desired format.
Please Try This
function display_c(){
var refresh=1000; // Refresh rate in milli seconds
mytime=setTimeout('display_ct()',refresh)
}
function display_ct() {
var CDate = new Date()
var NewDate=CDate.toDateString();
NewDate = NewDate + " - " + CDate.toLocaleTimeString();
document.getElementById('ct').innerHTML = NewDate;
display_c();
}
<html>
<head> </head>
<body onload=display_ct();>
<span id='ct' ></span>
</body>
</html>
For Change Date Or Time Format Please Refer this link
Get currentDate by toDateString() method and get current time by toLocaleTimeString and append both in your DOM . And call this every 1000 ms to get updated value .
function callDateTime(){
var currentDate=(new Date()).toDateString();
var currentTime=(new Date()).toLocaleTimeString();
document.getElementById('watch').innerHTML=`${currentDate}-${currentTime}`;
}
setInterval(function(){ callDateTime() }, 1000);
<p id="watch"></p>
You can add an HTML to your the body of your document:
Also, you can add a JavaScript similar to this before </body>:
Code:
function clock(){
window.rt=1000;r=0;
document.getElementById('t-0').textContent=new Date().toLocaleDateString(); // today
var m=setInterval(function(){
if(r>84600){clearInterval(m);}
r++;
document.getElementById('t-2').textContent=new Date().toLocaleTimeString(); // clock
}, window.rt);
}
<!DOCTYPE html>
<html lang="en">
<title>Clock</title>
<body onload="clock();">
<div style="text-align:center;">
📅
<b id="t-0">00/00/0000</b>
⏰
<b id="t-2">00:00:00</b>
</div>
</body>
</html>
I am trying to learn web programming and Javascript.
I'm trying to take whatever the user copied from an Excel spreadsheet and create a JavaScript spreadsheet/table that includes dropdowns out of it.
So basically the user would paste the input into a blank field, press the submit button, and JavaScript would generate an Excel-like spreadsheet which would have the data sorted accordingly. The dropdowns would be at the top of each column and allow the user to select either ready/not ready. This is being designed for a confluence WIKI page.
This is what I have so far, right now the code parses the input-field and creates a multi-dimensional array. After that, the array will be logged on the console. I need help getting everything displayed on the wiki page.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<textarea id="textarea"></textarea>
<button id="loadPaste">parse</button>
<script type="text/javascript">
var excel = new Array();
$(document).ready(function() {
$('#loadPaste').click(function() {
var text = $('#textarea').val();
var rows = text.split("\n");
for (var i = 0; i < rows.length; i++) {
excel[i] = rows[i].split("\t");
}
console.log(excel);
});
});
</script>
</body>
</html>
Lets close this one: //THIS IS #Downgoat response with little explanation about xml parser.
use an xml parser var xml = (new DOMParser()).parseFromString($('#textarea').val(), 'text/xml')
var text, parser, xmlDoc;
text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
<html>
<body>
<p id="demo"></p>
</body>
</html>
I am hoping you can help.
I am trying to use a date format field in HTMl, which will then use this date in JavaScript. Once it has read the date, it will work out whether it is within 3 months or not.
At the moment, it is returning saying invalid date entered. How do I get JavaScript to read the date when using a HTML date format?
<!DOCTYPE html>
<html>
<head>
<title>ICT5</title>
<script>
function checkdate() {
var UserDate = document.getElementById("date").value;
var parts = UserDate.split("/");
var dt = new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10),
parseInt(parts[0], 10));
window.alert("userdate is" +dt);
var timestamp = new Date().getTime() + (90 * 24 * 60 * 60 * 1000);
window.alert("" +timestamp);
if (timestamp > dt){
alert("Selected time is less than 3 months from now");
} else {
alert("Selected time is more than 3 months from now");
}
}
</script>
</head>
<body>
<h1>ICT 5 a</h1>
<h1> Working out the difference between today and your selected date </h1>
<p> Please enter a date </p>
<input id="date" type="date">
<button type="button" onclick="checkdate()">Submit</button>
</body>
</html>
I am new to JavaScript and I am trying to make a date-picker widget, I have the selected date in mm/dd/yy format,how can I get the date as "Thu(Day),25th July(Date,month) ,2013" kind of format and also how to set the input value to the current date.Here's my fiddle,
http://jsbin.com/idowik/3/
http://jsbin.com/idowik/3/edit
There are so much warnings , please bear with me and please open the output in new tab. Thank You,
Read the docs about the Date Object: http://www.w3schools.com/jsref/jsref_obj_date.asp.
Everything you need is described there.
Have a look at the following code:
<!DOCTYPE html>
<html>
<body>
<input id="dateInput" type="text"></input>
<button id="convertButton">Convert to string</button>
<a id="dateString"></a>
<script type="text/javascript">
var dateInput = document.getElementById("dateInput");
var button = document.getElementById("convertButton");
var dateString = document.getElementById("dateString");
var date;
button.onclick = function() {
var year = 2000+ parseInt(dateInput.value.split("/")[2]);
var month = dateInput.value.split("/")[0];
var day = dateInput.value.split("/")[1];
date = new Date(year, month, day);
dateString.innerHTML = date.toDateString();
}
</script>
</body>
</html>
This html page does exactly what you want.