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);
});
Related
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.
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>
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>
I've got some coffee script in /assets/javascripts/transactions.coffee that populates a text field (in /views/transactions/_form.html.erb) with today's date in dd/mm/yy format.
$ ->
today = new Date
dd = today.getDate()
mm = today.getMonth() + 1
#January is 0!
yyyy = today.getFullYear()
if dd < 10
dd = '0' + dd
if mm < 10
mm = '0' + mm
today = dd + '/' + mm + '/' + yyyy
$('#transaction_date').val today
$ ->
$('#transaction_date').datepicker({dateFormat: "dd/mm/yy"})
Currently, that form is referenced in /views/transactions/new.html.erb and /views/transactions/edit.html.erb. However, I only want the text field to be populated on new.html.erb and not edit.html.erb.
What's the 'cleanest' way to achieve this in Rails (4.2.5)?
Maybe you can create functions in transactions.coffee that you call from your view with an inline script. For example, in transactions.coffee
this.App || (this.App = {})
this.App.Transactions =
runForNewView: () ->
today = new Date
dd = today.getDate()
mm = today.getMonth() + 1
#January is 0!
yyyy = today.getFullYear()
if dd < 10
dd = '0' + dd
if mm < 10
mm = '0' + mm
today = dd + '/' + mm + '/' + yyyy
$('#transaction_date').val today
Then for example in the new view template, you would just add
<script text="text/javascript">
$(function() { App.Transactions.runForNewView(); });
</script>
I want to compare the user's birthday against today's date and get the number of days in between. The birthday they enter will be in the form of 12/02/1987 in an input box of type text
In my JS file I have code that looks like this:
function validateDOB(element) {
var valid = false;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //do that January is NOT represented by 0!
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd
}
if (mm < 10) {
mm = '0' + mm
}
var today = mm + '/' + dd + '/' + yyyy;
alert(today);
if (element.value != today) {
var days = 0;
var difference = 0;
Christmas = new Date("December 25, 2011");
today = new Date();
difference = today - Christmas
days = Math.round(difference / (1000 * 60 * 60 * 24)-1);
alert(days);
valid = true;
}
Instead of using "Christmas" I want to compare element.value... how do I do this?
When I put difference = today - element.value it won't show me the difference. The alert box comes up as NaN.
I wrote a lightweight date library called Moment.js to handle stuff like this.
var birthday = moment('12/02/1987', 'MM-DD-YYYY');
var inputDate = moment(element.value, 'MM-DD-YYYY');
var diff = birthday.diff(inputDate, 'days');
http://momentjs.com/docs/#/displaying/difference/
You'll need to first parse element.value as a date:
difference = today - new Date(element.value);
http://jsfiddle.net/gilly3/3DKfy/