document.getElementById is null? ... what's wrong with my code..? [duplicate] - javascript

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 years ago.
Why am I getting "document.getelementbyId is null" ???
Any help is appreciate. thx. ... (and it says I need to type more text before I can submit... )
// get current date - in format
function thisDate(){
var today = new Date(),
dd = today.getDate(),
mm = today.getMonth() + 1,
yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = mm + ',' + dd + ',' + yyyy;
dateDisplay = document.getElementById('dateDisplay')[0];
dateDisplay.innerHTML = today.toString();
console.log(today);
}
thisDate();

Remove [0] from your document.getElementById('dateDisplay')[0];
document.getElementById does not return an array, so there is no point in having an index pointer.

Related

Add 10 more days to new Date Javascript [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 11 months ago.
So I'm getting the date to this format like this: 12.12.2023 with this function:
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = dd + '.' + mm + '.' + yyyy;
And what I want to do is for example, add more 10 days to it. for example, if it is: 12.12.2023 to be 22.12.2023
let days = 10;
today.setDate(today.getDate() + days);

Convert timestamp to yyyy-mm-dd hh:mm:ss format using javascript [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
How to format a Date in MM/dd/yyyy HH:mm:ss format in JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I need to convert the current timestamp (Eg: 1578293326452) to yyyy-mm-dd hh:mm:ss format
using javascript.
I obtained the current timestamp as follows:
var date = new Date();
var timestamp = date.getTime();
How can I change the format?
function getTime(){
var date = new Date();
var year = date.getFullYear();
var month = (date.getMonth() +1);
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
return formateTime(year, month, day, hour, minute, second);
}
function formateTime(year, month, day, hour, minute, second){
return makeDoubleDigit(year) + "-" +
makeDoubleDigit(month) + "-" +
makeDoubleDigit(day) + " " +
makeDoubleDigit(hour) + ":" +
makeDoubleDigit(minute) + ":" +
makeDoubleDigit(second);
}
function makeDoubleDigit(x){
return (x < 10) ? "0" + x : x;
}
console.log(getTime())
Maybe this is what you need
d = Date.now();
d = new Date(d);
d = (d.getMonth()+1)+'/'+d.getDate()+'/'+d.getFullYear()+' '+(d.getHours() > 12 ? d.getHours() - 12 : d.getHours())+':'+d.getMinutes()+' '+(d.getHours() >= 12 ? "PM" : "AM");
console.log(d);

How to subtract date with date in javascript [duplicate]

This question already has answers here:
Get difference between 2 dates in JavaScript? [duplicate]
(5 answers)
Closed 8 years ago.
I got current day by code below
var currentDate = new Date();
var year = currentDate.getFullYear();
var month = currentDate.getMonth()+1;
if (month < 10) month = "0" + month;
var day = currentDate.getDate();
if (day < 10) day = "0" + day;
currentDate = day + "-" + month + "-" + year;
Now I have input type date.
So I want the date I add into the input subtract to currentDate
what should I do?
Tks for your help!
Sorry if my english's too bad
Just include momentjs, it makes rhings easier.
var current = moment();
$('#datepicker').change(function(e){
var d = moment.duration(current.diff(moment($(e.currentTarget).val())));
var s = "days: " + d.days() + " hours: " + d.hours() + " minutes: " + d.minutes();
$('#result').html(s);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.js"></script>
<input type='date' id='datepicker' />
<div id='result'>
</div>

javascript date format conversion [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I am using pikaday's datepicker. It gives me output "Fri Sep 20 2013". How can I convert this date into yyyy-mm-dd format and I also would want following date of this selected date and set that one to another element.
I tried this code
function formattedDate() {
var fromdate = new Date(document.getElementById('datepicker').value);
var dd = fromdate.getDate();
var mm = fromdate.getMonth()+1; //January is 0!
var yyyy = fromdate.getFullYear();
if(dd < 10)
{
dd = '0'+ dd;
}
if(mm < 10)
{
mm = '0' + mm;
}
var fromdate1 = dd+'/'+mm+'/'+yyyy;
fromdate.setDate(fromdate.getDate() + 2);
document.getElementById('datepicker').value = fromdate1;
var newdate = fromdate;
document.getElementById('datepicker1').value = newdate;
//alert(newdate1);
}
But it doesn't work.
var date = new Date(dateString);
var year = date.getFullYear(), month = (date.getMonth() + 1), day = date.getDate();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var properlyFormatted = "" + year + month + day;
Or
var date = new Date(dateString);
var properlyFormatted = date.getFullYear() + ("0" + (date.getMonth() + 1)).slice(-2) + ("0" + date.getDate()).slice(-2);
Use momentjs — it's a library available for using in browser and node projects
In your case you should use this pattern:
moment().format("YYYY-mm-D");
And you can try it in console on momentjs's site:

Javascript change date format from YYYY-MM-DD HH:MM:SS to MM-DD-YYYY [duplicate]

This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 9 years ago.
I want to change YYYY-MM-DD HH:MM:SS to MM-DD-YYYY
Example:
I have a string which represents a date in this format: 2013-06-15 03:00:00
I want to change this string into 06-15-2013 using JavaScript.
Is there any library to do this? or should i just use JavaScript?
function change(time) {
var r = time.match(/^\s*([0-9]+)\s*-\s*([0-9]+)\s*-\s*([0-9]+)(.*)$/);
return r[2]+"-"+r[3]+"-"+r[1]+r[4];
}
change("2013-06-15 03:00:00");
see moment.js.. i found it very useful
http://momentjs.com/
DEMO: http://jsfiddle.net/abc123/f6k3H/1/
Javascript:
var d = new Date();
var c = new Date('2013-06-15 03:00:00');
alert(formatDate(c));
alert(formatDate(d));
function formatDate(d)
{
var month = d.getMonth();
var day = d.getDate();
month = month + 1;
month = month + "";
if (month.length == 1)
{
month = "0" + month;
}
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
return month + '-' + day + '-' + d.getFullYear();
}
Since this doesn't use RegEx you'll notice some weirdness....Examples:
d.getMonth() + 1
this is because getMonth is 0 indexed....
day = day + "";
if (day.length == 1)
{
day = "0" + day;
}
this is because hours, seconds, minutes all return 1 digit when they are 1 digit...this makes them return a leading 0. The same can be done to Month and Day if desired.

Categories

Resources