How to make a date that can rollover months and years - javascript

I want to create an incremental date. I have two buttons, one increases the date by 1 and another decreases it by 1. It works well until the end of the month or year; how to make it so it rolls over and can change depending on the month, as some months don't have the same amount of days?
Here's the code I have thus far
var today = new Date();
var dd = today.getDate()+days;
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = dd + '/' + mm + '/' + yyyy;
$("#label").text(today);
days is a variable I'm using to add the days to the function

Just add the days directly to the Date, it will handle it:
var today = new Date();
today.setDate(today.getDate() + days);
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();

You can also use this example:
<html>
<head>
<title>Sample</title>
</head>
<body>
<div id="label">
</div>
<button type="button" onclick="add()" name="button">Add</button>
<button type="button" onclick="sub()" name="button">Subtract</button>
</body>
<script type="text/javascript">
var days = 0;
var core = function(){
var today = new Date();
today.setDate(today.getDate() + days);
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) dd = '0'+dd
if(mm<10) mm = '0'+mm
$("#label").text(dd + '/' + mm + '/' + yyyy);
};
function add(){
days = days + 1;
core();
}
function sub(){
days = days - 1;
core();
}
add();
</script>
</html>

Related

set the date and time to current date and time

I want to limit the user from selecting the current date and current time.
What I have tried is:
<input id="datefield" type="date" />
<input type="time" id="timefield" class="form-control" />
var today = new Date();
var thishr = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
var hr = today.getHours();
var mn = today.getMinutes();
if(dd < 10){
dd = '0' + dd
}
if (mm < 10){
mm = '0' + mm
}
today = yyyy + '-' + mm + '-' + dd;
thishr = hr + '-' + mn;
document.getElementById("datefield").setAttribute("max", today);
document.getElementById("datefield").setAttribute("min", today);
document.getElementById("timefield").setAttribute("max", thishr);
document.getElementById("timefield").setAttribute("min", thishr);
Seems like the JavaScript for the date part is working, but the part for the time is not working as expected. Did I miss some important part?
first:
hr and mn have to include leading zeros, and the separator should be a colon, not a hyphen. like this : "08:05".
second:
put your input into a form element with a submit button. you will still be able to select any time, but when you will try to submit the form, the validation won't let you if the selected time is out of range.

How to change the format of full date

Actually I made a function in which picks up the current date. Now i want to pickup the date after 6 months of my current date
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 = mm + '/' + dd + '/' + yyyy ; //Here I am getting current date now i want to get the date exact after 6 months in the same format like this
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here I am getting date after 6 months but it is in different format like complete date, I want date after after 6 months like above format
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 = mm + '/' + dd + '/' + yyyy;
var date=today;
var x = 6; //or whatever offset
var CurrentDate = new Date();
//alert(CurrentDate);
CurrentDate.setMonth(CurrentDate.getMonth() + x);
var d = String(CurrentDate.getDate()).padStart(2, '0');
var m = String(CurrentDate.getMonth() + 1).padStart(2, '0');
var yy = CurrentDate.getFullYear();
CurrentDate = m + '/' + d + '/' + yy;
var months=CurrentDate;
Just pass new generated timestamp in new Date() and it will give you new date
Like var today_after_month = new Date(timestamp);
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();
var today = mm + '/' + dd + '/' + yyyy ; //Here i m getting current date now i want to get the date exact after 6 months in the same format like this
console.log('today', today);
var CurrentDate = new Date();
var month=CurrentDate.setMonth(CurrentDate.getMonth() + 6);//here i m getting date after 6 months but it is different format like complete date i want date after after 6 months like above format
var today_after_month = new Date(month);
var dd = String(today_after_month.getDate()).padStart(2, '0');
var mm = String(today_after_month.getMonth() + 1).padStart(2, '0');
var yyyy = today_after_month.getFullYear();
var today = mm + '/' + dd + '/' + yyyy ;
console.log('month', today);

How to show current date on input field date [duplicate]

This question already has answers here:
How to set input type date's default value to today?
(43 answers)
Closed 4 years ago.
I want to show current date on input field date.My code is down below
HTML:
<div class=\"col-lg-4\">
<label>Date</label>
<input type=\"date\" name=\"dateto1\" id=\"dateto1\" class=\"form-control\">
</div>
JAVASCRIPT:
<script type="text/javascript">
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
// today = yyyy + '/' + mm + '/' + dd;
today = mm + '/' + dd + '/' + yyyy;
console.log(today);
document.getElementById('dateto1').value = today;
}
window.onload = function() {
getDates();
};
</script>
this code is working fine if i use input type "text" but for type date its not working.Please guide me to solve this issue.
Thanks
You should use the yyyy-MM-dd format:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
// today = yyyy + '/' + mm + '/' + dd;
today = yyyy + '-' + mm + '-' + dd;
console.log(today);
document.getElementById('dateto1').value = today;
<div class="col-lg-4">
<label>Date</label>
<input type="date" name="dateto1" id="dateto1" class="form-control"></div>
The problem may be with the date format Use yyyy-mm-dd format.
Also there is no getDates function in the code
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
// today = yyyy + '/' + mm + '/' + dd;
today = `${yyyy}-${mm}-${dd}`;
document.getElementById('dateto1').value = today;
<div class="col-lg-4">
<label>Date</label>
<input type="date" name="dateto1" id="dateto1" class="form-control">
</div>

Split Date.now() into parts in Javascript

I am trying to split the current date into parts and this is what I am doing
var today = new Date();
var currDay = today.getDay();
var currMonth = today.getMonth();
var currYear = today.getYear();
alert(currDay + "/" + currMonth + "/" + currYear)
but with this I get the following result
0/7/115 (output)
where it should be
09/07/2015
What can I do to get the required result?
Your "today.getDay()" should be "today.getDate()" since .getDay goes from 0-6 (days of the week)
var today = new Date();
var currDay = today.getDate();
var currMonth = today.getMonth();
var currYear = today.getFullYear();
alert(currDay + "/" + currMonth + "/" + currYear);
Try
var currYear = today.getFullYear();
Based on the answer from https://stackoverflow.com/a/3605248/2649340:
var today = new Date();
var output = ('0' + today.getDate()).slice(-2) + '/'
+ ('0' + (today.getMonth()+1)).slice(-2) + '/'
+ today.getFullYear();
getDay() gives you the day's number (0-6 for sunday to monday), month starts with 0 for january. This is how to do:
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
if(day<10) {
day='0'+day;
}
if(month<10) {
month='0'+month;
}
today = month+'/'+day+'/'+year;
alert(today);
you can use moment.js. It has a lot of helpful functions for time and date in JavaScript

How to use alert if the system date is changed?

How can I use alert if the date of system is changed in javascript. The date can be change manually of automatically.
[Thought is, Mainly client want to get an notification after day of end and start of the day. Total sales of today will be notified to the client when tomorrow is starting. like if the date of 30 and when it will be 31 and mail will be send to the client that you have sold sum quantity yesterday or date of 30]
Here I have tried this code. I am new in javascript so suggestion is needed.
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
today = mm + '/' + dd + '/' + yyyy;
$("#today").change(function () {
alert(today);
});
You cannot listen to the event like you a trying, the only way you can do this is with a synthetic event that is fired when the date changes:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var changedate = function(date, to){
date = to;
document.trigger('dateChange');
}
document.on('dateChange', function () {
alert(today);
});

Categories

Resources