HTML input prepopulate date - javascript

Am rediscovering HTML after quite some time.
I am using just HTML and javascript along with Salesforce. I have two date input fields.
I was curious to see if there is any easy way to populate these fields with:
a. Today's date
b. Date 6 months before today.
<input type="text" id="toDate" size="10" onmouseover="initialiseCalendar(this, 'toDate')"/>
Thanks,
Calvin

The following JavaScript sets the value of your textbox to today's date, in format yyyy-mm-dd. See how I add 1 to the month? getMonth() returns 0-11 for current month, so 1 is added to it:
var today = new Date();
document.getElementById("toDate").value = today.getFullYear() + "-" +
parseInt(today.getMonth()+1) + "-" + ​​​​​​today.getDate();​​​​​
DEMO: Fiddle
It is worth noting though that if the month or day are lower than 10, you'll only get one digit for each of them. Let me know if this is an issue.
EDIT: To get 6 months from today, use:
var today = new Date();
var past = today.setMonth(today.getMonth() - 6);

Populate with today's date:
var today = new Date();
document.getElementById("toDate").value = today.getFullYear() + "-"
+ String(today.getMonth() + 101).slice(-2) + "-"
+ String(today.getDate() + 100).slice(-2);
6 month in the past:
var past = new Date();
past.setMonth(past.getMonth() - 6); //
document.getElementById("toOldDate").value = past.getFullYear() + "-"
+ String(past.getMonth() + 101).slice(-2) + "-"
+ String(past.getDate() + 100).slice(-2);

Related

Add 1 month to current date in JavaScript (Can you please integrate your solution/answer to this code and show) [duplicate]

This question already has answers here:
How to add months to a date in JavaScript? [duplicate]
(3 answers)
Closed 2 years ago.
$(document).ready(function () {
//Init
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear() + "-" + (month) + "-" + (day);
$('#PaymentDate').val(today);
});
I'm new to JavaScript So can someone help me. using this code I can get the current date...But, how can I add one month to this date and get the next month date..
Currently I'm getting this - 12/30/2020 (Today's Date) Add 1 month Means I want to get - 01/30/2021 (Next month date).
Can you please integrate your solution/answer to this code and show
Try this
$(document).ready(function () {
//Init
var now = new Date();
// Add one month to the current date
var next_month = new Date(now.setMonth(now.getMonth() + 1));
// Manual date formatting
var day = ("0" + next_month.getDate()).slice(-2);
var month = ("0" + (next_month.getMonth() + 1)).slice(-2);
var next_month_string = next_month.getFullYear() + "-" + (month) + "-" + (day);
$('#PaymentDate').val(next_month_string);
});
You can also use this trick to get your YYYY-MM-DD style string instead of the manual formatting:
var next_month_string = next_month.toISOString().split('T')[0];

how to shift a date to the following month (fixed day)

I have a date in javascript that is dd/mm/yyyy format.
I wanto to calculate from that date another date with the following requisites:
month will be the following from the date chosen (january will shift to february, dicember to january and so on)
year will shift accordingly
day will be always 16
I am starting from this (ref. this question):
var CurrentDate = new Date();
CurrentDate.setMonth(CurrentDate.getMonth() + 1);
console.log("Date after 1 months:", CurrentDate);
but need to set the day as 16 and to check that the edge situations are considered.
Any suggestion on how to go on with this code?
P.S. i'm looking for a solution in Vanilla JS or jQuery with no additional libraries
Try this..
function getDate16(d) {
var date1 = d.split('/');
var formattedDate1 = date1[2] + '-' + date1[1] + '-' + date1[0] + 'T00:30:00.000Z';
var CurrentDate = new Date(formattedDate1);
CurrentDate.setDate(16);
CurrentDate.setMonth(CurrentDate.getMonth() + 1);
// Date formatting
var date = CurrentDate.toISOString();
var arr = date.substring(0, 10).split("-");
date = arr[2] + '/' + arr[1] + '/' + arr[0];
console.log("ISO format=", CurrentDate);
console.log("dd/mm/yyyy format=", date);
}
getDate16("28/11/2019");
getDate16("31/12/2019");
getDate16("16/01/2020");

How to disable html date input type in choosing future dates?

I have a code and just last year, it was still working. What it does is that it disables user to pick future dates. Now that it's January, it doesn't seem to be working. I do not understand why. Please help me fix this. Your help will be appreciated.
This is the input:
<input id="reqDate" class="form-control col-md-7 col-xs-12" name="reqDate" required="required" type="date">
Javascript:
<script type="text/javascript">
var input = document.getElementById("reqDate");
var today = new Date();
var day = today.getDate();
// Set month to string to add leading 0
var mon = new String(today.getMonth()+1); //January is 0!
var yr = today.getFullYear();
if(mon.length < 2) { mon = "0" + mon; }
var date = new String( yr + '-' + mon + '-' + day );
input.disabled = false;
input.setAttribute('max', date);
</script>
By the way, I am using this code in a .php file.
Like you have used, the native input[type="date"] element supports a maximum date value:
Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31">
http://www.w3schools.com/tags/att_input_max.asp
The JavaScript you are using to set the date does not zero-pad the day, so for the first of January, you would get the date max="2016-01-1". To keep with your JavaScript version, sero pad the date with:
<script>
var input = document.getElementById("reqDate");
var today = new Date();
var day = today.getDate();
// Set month to string to add leading 0
var mon = new String(today.getMonth()+1); //January is 0!
var yr = today.getFullYear();
if(mon.length < 2) { mon = "0" + mon; }
if(day.length < 2) { dayn = "0" + day; }
var date = new String( yr + '-' + mon + '-' + day );
input.disabled = false;
input.setAttribute('max', date);
</script>
With this being a PHP script, to simplify your code you could use:
<input type="date" name="bday" max="<?=date('Y-m-d')?>">
N.B. Support for input[type="date"] varies between browsers (see http://caniuse.com/#feat=input-datetime).
You can confirm the max value used is in the correct supported RCF3339 format by inspecting the element in the browser inspector.
Did you try using min attribute in the input. In html5 we can specify min or max attributres in proper date format YYYY-MM-DD

Convert html5 date input to date object

I have a form setup with html5 date input what I wanna do is get that date add some days to it and then show the final result in another html5 date input
In my code here, the days are added to todays date.
how do I alter the Javascript so that the days are added to the user selected date.
current JS i am using:
var terms = $("#terms").val();
var date = new Date();
date.setDate(date.getDate() + terms);
var day = ("0" + date.getDate()).slice(-2);
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var final = date.getFullYear()+"-"+(month)+"-"+(day);
$("#duedate").val(final);
You need to parse your terms val as an integer - parseInt(terms). Fiddle.
$('#terms').on('blur', function() {
var terms = $("#terms").val();
var date = new Date($("#date").val());
date.setDate(date.getDate() + parseInt(terms));
var day = ("0" + date.getDate()).slice(-2);
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var final = date.getFullYear()+"-"+(month)+"-"+(day);
$("#duedate").val(final);
});

Shorter way to write this JavaScript date code

Hello I want to get today's date in JavaScript in the following format: dd/mm/YYYY
The following code does the job but surely there is a shorter way to write this?
var today = new Date();
var day = today.getDate();
var month = today.getMonth()+1;
var year = today.getFullYear();
var todaysDate = day + "/" + month + "/" + year;
Thanks in advance.
recommend a great js date/time lib: http://momentjs.com/
moment().format('D/M/YYYY'); //"10/2/2013"
if you need padding zeros:
moment().format('DD/MM/YYYY'); //"10/02/2013"
if you want write pure js, nothing much can do here, just cut off some variable.
var today = new Date;
//parenthesis around month is required.
var todaysDate = today.getDate() + '/' + (today.getMonth()+1) + '/' + today.getFullYear();
//"10/2/2013"
Of course.
var date = new Date();
var todaysDate = date.getDate() + "/" + date.getMonth()+1 + "/" + date.getFullYear();
Note that this will return values without leading zeros. You can add them using one extra function for convenience:
function checkTime(i){if(i<10){i="0" + i}return i}
var date = new Date();
var todaysDate = checkTime(date.getDate()) + "/" + checkTime(date.getMonth()+1) + "/" + date.getFullYear();
Although JavaScript provides a bunch of methods for getting and setting parts of a date object, it lacks a simple way to format dates and times according to a user-specified mask. Yours is quite simple enough.
You could use an external library, but the script size trade-off won't be optimal for just one simple operation.
A shorter version would be to combine the last 4 lines inline this way:
var today = new Date();
var todaysDate = today.getDate() + "/" + (today.getMonth()+1) + "/" + today.getFullYear();

Categories

Resources