I'm building a form to standardize filenames (I'm a video editor). After a lot of research, copying, pasting and testing I'm almost there. I just need to display the current date at the end of the filename after the user clicks on the corresponding checkbox.
The HTML has the code to get the current date and to format it as I want (YYMMDD), but for the life of me I can't find a way to display it at the end of the filename. The code to display the date works because I can enable/disable text, but I can't display the result of the todaysdate function.
This is the code to get the current date and format it to YYMMDD:
function SetDate()
{
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear() - 2000;
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var today = year + month + day;
document.getElementById('today').value = today;
}
This is the code that adds or removes the date at the end of the filename when you click the checkbox.
function todaysdate()
{
var checkbox = document.getElementById('todayis');
if (checkbox.checked != false)
document.getElementById('todayis').value = "DATE";
if (checkbox.checked != true)
document.getElementById('todayis').value = "";
}
This is the code for the checkbox:
Add date (YYMMDD): <input type="checkbox" onclick="todaysdate()" id="todayis" value="" />
Thanks in advance for your help.
Edit: Added the code.
In your todaysdate function you set the value of the todayis input to DATE, where you should be setting it to the value of your date calculation. Here’s just a little change to what you have that should probably work!
function todaysdate()
{
var checkbox = document.getElementById('todayis');
if (checkbox.checked != false)
document.getElementById('todayis').value = getDate();
else if (checkbox.checked != true)
document.getElementById('todayis').value = "";
}
function getDate()
{
var date = new Date();
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear() - 2000;
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var today = year + "" + month + "" + day;
return today;
}
You can also use the php function Time:
Set the timezone:
date_default_timezone_set('America/New_York');
And get the actual date:
$date_now = date('Y/m/d', time());
So, to set the value on the input:
<input type="checkbox" value="<?php echo $date_now ?>" />
function getYYMMDDDate(date) {
var year = String(date.getFullYear()).substring(2,4);
var month = String(date.getMonth() + 1).padStart(2, "0");
var day = String(date.getDate()).padStart(2,"0");
return year + month + day;
}
...
// change this document.getElementById('todayis').value = "DATE";
document.getElementById('todayis').value = getYYMMDDDate(new Date());
Related
I'm trying to convert a MM/DD/YYYY date to a long date. So for example, 02/16/2020 would convert to something like 16/02/2020.
Is there a way to make this date conversion accurately?
You need to specify the original format of the time, and then convert it to a new format.
const date = "02/16/2020";
alert(moment(date, "MM/DD/YYYY").format('DD/MM/YYYY'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Use moment for date formatting:
Sample Code:
moment('02/16/2020').format('16/02/2020');
You can play with date by moment.js. It is very useful tool for javascript developer.
Momemet Js Document
For dynamic value:
moment(yourDate, 'MM/DD/YYYY').format('DD/MM/YYYY');
Here, yourDate is your dynamic value date.
check this. its work.
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [day,month,year].join('/');
}
document.getElementById('res').innerHTML = formatDate('02/16/2020') ;
<div id="res">res</div>
2 || 1 liners ?
var src = '02/16/2020'
var a = src.split('/');
console.log(a.concat(a.splice(0, 2)).join('/'));
console.log(src.replace(/(\d+)\/(\d+)\/(\d+)/, '$3/$1/$2'));
If you want a conversion just between the exact formats you have mentioned:
function dfConvert(f) {
var farr = f.split("/");
return `${farr[1]}/${farr[0]}/${farr[2]}`;
}
var input = "02/16/2020";
console.log(`input: ${input}`)
console.log(`output: ${dfConvert(input)}`);
If you want the actual date object and from that you want your mentioned format for some reason:
function toDate(f) {
var farr = f.split("/");
return new Date(parseInt(farr[2]), parseInt(farr[0])-1, parseInt(farr[1]))
}
function dfConvert(f) {
var d = toDate(f)
var day = d.getDate()
var month = (d.getMonth() + 1)
var year = d.getFullYear()
return `${((day.toString().length <= 1) ? "0": "")}${day}/${((month.toString().length <= 1) ? "0": "")}${month}/${year}`
}
var input = "02/16/2020"
console.log(`input: ${input}`)
console.log(`output: ${dfConvert(input)}`)
Hope it helps
I have an input containing a date value
example:
<input type="text" name="cel_dafa" value="26/12/2018">
I want a button when pressed it increases the value one month
So that the result after the pressure:
<input type="text" name="cel_dafa" value="26/1/2019">
Note: I do not want to harm the day,
Only the month and year if at the end of the year
Ok, so consider you have a button like;
<button onclick="increase()"> Click Me </button>
And input like;
<input type="text" id="date" name="cel_dafa" value="26/12/2018">
Now code of increase function;
function increase()
{
var currentDate = document.getElementById('date').value;
var parts = currentDate.split("/");
var day = parts[0];
var month = parts[1];
var year = parts[2];
var d = new Date(year, month - 1, day);
d.setMonth(d.getMonth() + 1);
var newDate = d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear();
document.getElementById('date').value = newDate;
}
Html
<input type="text" id="date-input" name="cel_dafa" value="26/12/2018">
<button onclick="increaseDate()">submit</button>
JS
function increaseDate() {
var dateInput = document.querySelector('#date-input').value;
var day = dateInput.split('/')[0];
var month = dateInput.split('/')[1];
var year = dateInput.split('/')[2];
if (month > 11) {
year = parseInt(year) + 1;
month = 1
} else {
month = parseInt(month) + 1;
}
var newDate = day +"/"+ month +"/"+ year;
document.querySelector('#date-input').value = newDate;
}
I have used input date type in my website. I want to disable current date by time.
For example
After 12:00:00 PM current date 2018-04-16 disabled.
I tried this code:
<!-- disable past date -->
$(function() {
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if (month < 10)
month = '0' + month.toString();
if (day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
//console.log(maxDate);
$('#demo_date').attr('min', maxDate);
$('#demo_date').attr('max', '2018-04-29');
});
<!-- disable past date -->
<!-- disable date by time -->
$(document).ready(function() {
var time = new Date().toLocaleTimeString();
if (time <= '12:00:00 PM') {
// code for disable demo_date
}
});
<!-- disable date by time -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input placeholder="Select Date" class="textbox-n" type="text" name="demo_date" onfocus="(this.type='date')" id="demo_date">
Ok you got it. I have made a demo for you here you will not able to select the current date after 12:00 pm. Cheers....
$(function() {
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if (month < 10)
month = '0' + month.toString();
if (day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
//console.log(maxDate);
var time = new Date().toLocaleTimeString();
if (time >= '12:00:00 PM') {
day = day+1
maxDate = year + '-' + month + '-' + day;
// code for disable demo_date
}
$('#demo_date').attr('min', maxDate);
$('#demo_date').attr('max', '2018-04-29');
});
<!-- disable past date -->
<!-- disable date by time -->
$(document).ready(function() {
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="Select Date" class="textbox-n" type="text" name="demo_date" onfocus="(this.type='date')" id="demo_date">
By the below result in browser console:
>> var time = new Date().toLocaleTimeString();
undefined
>> time
"11:27:28 AM"
>> time <= '12:00:00 PM'
true
code works fine.But you should define the other part of expression. you should define :
if( time >= some_date_value && time <= some_date_value )
{
// code for disable demo_date
}
This is my code and I want to add two days from the current date to the value of a hidden input. If I borrow now, this results in a waiting period of two days. It will be better if I borrow on Friday; Saturdays and Sundays will not count so the waiting period ends on Monday, four days later.
<input type="hidden" name="due_date" id="sd" maxlength="10" style="border: 3px double #CCCCCC;" required/>
You can use JavaScript to add 2 days and For Friday(5) add 4 days to skip Saturday And Sunday plus 2 days:
var currentDate = new Date();
//Checking If Current day is Friday
if(currentDate.getDay() == 5) {
var numberOfDaysToAdd = 4; //Adding 4 to skip sat. & sun. if Friday
} else {
var numberOfDaysToAdd = 2; //Adding 2 days if not Friday
}
currentDate.setDate(currentDate.getDate() + numberOfDaysToAdd);
//Formatting to dd/mm/yyyy :
var dd = currentDate.getDate();
var mm = currentDate.getMonth() + 1;
var y = currentDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;
// Displaying Fromatted Date
document.getElementById("display").innerHTML = someFormattedDate;
<div id="display"></div>
It has been assumed that nothing is Borrowed on Saturday And Sunday.
The code creates an array of objects referring to the days of the week as well as a Date object oDate used to retrieve the current date information. If the day of the week is not Friday, then user is advised to wait till Friday.
The hidden input "due_date" has its value set to two days from the current date unless that day is Friday in which case the due date becomes 4 days later, to skip the weekend and add the usual 2 days to the waiting period. If the hidden input were part of a form, once it is submitted, and the data validated, assuming submission by POST, one could use variable $_POST["due_date"] in an INSERT query to store that value in a database, making sure to use either mysqli_real_escape_string() or PDO and bound parameters.
Note: I altered the HTML so that both the NAME and ID attributes of the hidden input are both set to "due_date".
var d = document;
d.g = d.getElementById;
var arrDaysOfWeek = {"Sunday":0,"Monday":1,"Tuesday":2,"Wednesday":3,"Thursday":4,"Friday":5,"Saturday":6};
var arrWkDayNames = Object.keys( arrDaysOfWeek );
var oDate = new Date();
var currDay = oDate.getDay();
var md = oDate.getDate();
var mm = oDate.getMonth() + 1;
var y = oDate.getFullYear();
var waitPeriod = 2; // default
var daysTillFriday = (currDay == 0)? arrDaysOfWeek["Friday"]
: arrDaysOfWeek["Friday"] - currDay;
if (currDay == arrDaysOfWeek["Saturday"]) {
daysTillFriday = arrWeekDayNames.length + arrDaysOfWeek["Friday"] - currDay;
}
var mess = "";
if (currDay != arrDaysOfWeek["Friday"] ) {
mess = "\nYou should wait to borrow on Friday, i.e. " + daysTillFriday + " days from today.";
}
if( currDay + 2 != arrDaysOfWeek["Friday"] ) {
daysTillFriday = arrDaysOfWeek["Friday"] - currDay - 2;
mess += "\nSo, best not even in two days. Just wait till Friday which will be in " + daysTillFriday + " days from two days from now.";
}
waitPeriod = (currDay == arrDaysOfWeek["Friday"] )
? 4 //skip sat. & sun. plus 2
: 2; // usual wait period
oDate.setDate(md + waitPeriod);
mess += "\nTo proceed know that the happening date is " + oDate;
//USA date style ...
var date_parts = [ mm, md, y ];
mess += "\nToday is " + arrWkDayNames[ currDay ] + ", " + date_parts.join("/");
d.g("display").textContent = mess;
d.g("due_date").value = oDate;
console.log( "Hidden input due date value: " + d.g("due_date").value );
<div id="display"></div>
<input type="hidden" name="due_date" id="due_date" maxlength="10" style="border: 3px double #CCCCCC;" required/>
You can use JavaScript, no jQuery required:
var someDate = new Date();
var numberOfDaysToAdd = 2;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
Formatting to dd/mm/yyyy :
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = dd + '/'+ mm + '/'+ y;
answer from - How to add number of days to today's date?
You can do it in php
echo date('Y-m-d', strtotime("+2 days"));
Answer From - Add number of days to a date
This is the actual content. While the below code, added a value in dropdown for two times in UI. Shows the dropdown value as "AprilApril" instead of "April"
if ($('#dk_container_selectToYear .dk_options li.dk_option_current a').text() == 2016) {
var currentdate = new Date();
var month = currentdate.getMonth();
var monthname = monthLookup[currentdate.getMonth()];
month = month + 3;
while (month <= 13) {
$('#dk_container_selectToMonth .dk_options li:nth-child(' + month + ')').css({ "display": 'none' });
var listvalue = $('#dk_container_selectToMonth .dk_options li:nth-child(' + month + ')').text();
if ($('#dk_container_selectToMonth .dk_options li.dk_option_current a').text() == listvalue) {
$('#selectToMonth').dropkick('reset');
$('#selectToMonth').dropkick('setValue', monthname);
}
month++;
}
}
$('#selectToMonth').dropkick('setValue', monthname);
Is it have any other option to avoid it..
Thanks in advance..
!!This is not an solution for your question but it is only a hack till you find the solution.
Just grab the half of the string.. like this:
var monthname = 'AprilApril';
var month = monthname;
console.log(month.substr(0, (month.length/2))); // April
console.log(month) // AprilApril
Enjoy coding!