In my user extensions i used this code to check the date but it keeps getting errors:
Selenium.prototype.doCheckDate = function(){
var dates = new Date();
var day = dates.getDate();
if (day < 10){
day = '0' + day;
}
month = dates.getMonth() + 1;
if (month < 10){
month = '0' + month;
}
var year = dates.getFullYear();
var prettyDay = day + '-' + month + '-' + year;
this.browserbot.getUserWindow().checkDate(prettyDay);
}
The error: [error] this.browserbot.getUserWindow(...).checkDate is not a function
Does anybody have an idea how i can check the current date or dates that are in the future? Our systems fills a datefield with a date that is one or two years from the current date.
Thanks for your time and effort in advance. I appreciate it.
It appears that all your function does is gets the current date. What is your ultimate goal? Do you intend to compare a the system date to a date from the field and verify it's 1 or 2 years later?
Related
I have set the date type input to display calendar with date not less than 18 years to avoid user choosing date that is less than 18 years old. But the problem is the user still able to manually type in a wrong date. Is there a way to show an alert message and empty the input when a date less than 18 years is typed in?
$(function(){
var dtToday = new Date();
var month = dtToday.getMonth() + 1;// jan=0; feb=1 .......
var day = dtToday.getDate();
var year = dtToday.getFullYear() - 18;
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var minDate = year + '-' + month + '-' + day;
var maxDate = year + '-' + month + '-' + day;
$('#emp_dob').attr('max', maxDate);
});
<div class="form-group">
<label>Date of Birth</label>
<input type="date" name="emp_dob" id="emp_dob" class="form-control">
</div>
I infer you are using jquery, so it's pretty easy, you need to do the following:
Define in which HTML tag you want to show this alert box.
Make the checkup.
Print the message.
if (year < 18) {
$('.error-div').html("You can't choose less than 18 years");
}
You can also use $.append.
Check the following links:
https://api.jquery.com/html/
https://api.jquery.com/append/#append-content-content
https://api.jquery.com/text/#text
Use the one that better fits your problem.
You already have the date picker not allowing a selection of a Date less than 18 years from today's date. So IMHO it doesn't seem feasible to have an alert for a selection that isn't possible.
But, if you would like to do that, you must create a new condition like so:
if ( year < 18 ) {
alert("You must be 18 years of age");
}
$(function(){
var dtToday = new Date();
var month = dtToday.getMonth() + 1;// jan=0; feb=1 .......
var day = dtToday.getDate();
var year = dtToday.getFullYear() - 18;
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var minDate = year + '-' + month + '-' + day;
var maxDate = year + '-' + month + '-' + day;
$('#emp_dob').attr('max', maxDate);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" referrerpolicy="no-referrer"></script>
<div class="form-group">
<label>Date of Birth</label>
<input type="date" name="emp_dob" id="emp_dob" class="form-control">
</div>
Every HTML <input> element fires a series of events. For instance if you add an change event listener to your date input, it would fire as soon as the user inputs something. So the basic idea is we catch if the user entered something and compare the input to a target date - in your case sometime before 18 years. The problem with this is that the event would fire every time the user changes a single number, not just once if the entire date is set. What we need to do instead is waiting for a press of the return key, as it's used to actually set the chosen date.
Inside the callback function we can compare the date to the target date and act accordingly - e.g. display an alert box.
Here's an example:
let minDate = new Date("2003-07-21");
document.getElementById("emp_dob").addEventListener("keyup", function(e) {
if (e.key == "Enter") {
if (new Date(e.target.value) > minDate) {
alert("Wrong date!");
}
}
});
<div class="form-group">
<label>Date of Birth</label>
<input type="date" name="emp_dob" id="emp_dob" class="form-control">
</div>
how can I get all the dates of the current and previous month in moment.js?
I was able to get the months but unable to find the dates of months
The below code if for getting months is there any similar code for getting dates also?
var labels = moment.monthsShort();
i want to show something like
this image
//sample to display february days
var months = moment.monthsShort();
var datesofmonth = createAllDatesOfMonth(months[1], 2021)
console.log(datesofmonth);
function createAllDatesOfMonth(month, year) {
let year_month = year + "-" + month;
let nbdays = moment(year_month, "YYYY-MMM").daysInMonth();
return Array.from(Array(nbdays).keys()).map((d) => month + " " + (d + 1));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
This question already has answers here:
Compare two dates with JavaScript
(43 answers)
Closed 2 years ago.
I am trying to compare two dates which works properly on every device except the iPhone.Its giving wrong alert on iPhone.
jsfiddle
function play_time() {
var crntTime;
var today = new Date();
var formattedTime = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
today = yyyy + '-' + mm + '-' + dd;
var crntDatetime = `${today} ${formattedTime}`;
crntTime = new Date(crntDatetime).getTime();
var check = '2020-08-18 23:14:07';
var gameTime = new Date(check).getTime();
if (crntTime <= gameTime) {
alert('Play');
} else {
alert('Later');
}
}
<button onClick="play_time();"> show Time </button>
By using a space between the date and time in your check example, you're straying outside of the specification. That means that the implementation can use local time, or UTC, or refuse to parse it entirely.
Use the date time format in the specification which is basically what you have but with a T instead of space, or use the multi-argument Date constructor (for local time), or Date.UTC (passing the result into the Date constructor) for UTC. If parsing a string ideally always specify Z for UTC or a timezone offset. Date-only forms ("2020-08-19") are parsed in UTC but date-time forms ("2020-08-19T00:00:00") are parsed in local time — but, that changed more than once during the years 2015-2017 so it's a bit risky to rely on it.
In your situation, since you already have the information as separate variables, I would definitely use the multi-argument version of new Date or Date.UTC+new Date (depending on whether you want local or UTC).
My database value is this
2020-03-08 20:44:00
But in javascript. It display
Mon Mar 09 2020 09:44:00 GMT+0800 (Singapore Standard Time)
Want i want to display on UI
2020-03-08 20:44:00
or
2020-03-08
Is there a way to remove the timezone and get only the actual value from the database.
toISOString is not a proper way to get date into DateTime. please follow the below method to get a date from DateTime.
var date = new Date("2020-03-08 20:44:00");
var year = date.getFullYear();
var month = (1 + date.getMonth()).toString();
month = month.length > 1 ? month : '0' + month;
var day = date.getDate().toString();
day = day.length > 1 ? day : '0' + day;
var newDate = year + '-' + month + '-' + day;
console.log("Date plush time - "+date);
console.log("Only Date - "+newDate);
You're using the silently using Date object's .toString() method which converts the UTC date (that your database is storing) into a time in the current time zone.
If date is the variable that you get from your database, then you can format it like you want it like this:
let dateString = date.toISOString().replace('T', ' ').replace(/\..+/, '')
This will take your date, convert it into an ISO string (in the form 2020-01-10T03:09:24.551Z) and replace the T with a space and everything after the decimal with nothing.
Try this.
let d = new Date('2020-03-08 20:44:00');
console.log(`${d.getFullYear()}-${d.getMonth() < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1}-${d.getDate() < 10 ? '0' + (d.getDate()): d.getDate()}`);
You can take each part of the date and construct your own format
example:
let formatted_date = my_date.getFullYear() + "-" + (my_date.getMonth() + 1) + "-" + my_date.getDate()
in this example: my_date hold the date you want to display.
If you're able to use a library, use moment.js
https://momentjs.com/docs/#/displaying/
moment("2020-03-08 20:44:00").format("YYYY-MM-DD");
or
moment(new Date("2020-03-08 20:44:00")).format("YYYY-MM-DD");
It can even change the time to utc
https://momentjs.com/guides/#/parsing/local-utc-zone/
moment.utc("2020-03-08 20:44:00").format("YYYY-MM-DD");
hope this helps :)
Subtract your timezone offset milliseconds.
var dt = new Date('2020-03-08 20:44:00');
dt = new Date(dt.getTime()-dt.getTimezoneOffset()*60000);
console.log(dt.toUTCString());
var mo = dt.getUTCMonth()+1, d = dt.getUTCDate(), h = dt.getUTCHours();
var m = dt.getUTCMinutes(), s = dt.getUTCSeconds();
if(mo < 10)mo = '0'+mo;
if(d < 10)d = '0'+d;
if(h < 10)h = '0'+h;
if(m < 10)m = '0'+m;
if(s < 10)s = '0'+s;
console.log(dt.getUTCFullYear()+'-'+mo+'-'+d+' '+h+':'+m+':'+s);
I have an string that contains month/date and I need to insert the year. The string looks like:
Last Mark:: 2/27 6:57 PM
I want to convert the string to something like:
Last Mark:: 2010/02/27 18:57
In this case, there will not be any entries more than a year old. For example, if the date were 10/12 it can be assumed that the year is 2009.
What is the best method for this?
Following from Adam's suggestion:
function convertDate(yourDate) {
var today = new Date();
var newDate = new Date(today.getFullYear() + '/' + yourDate);
// If newDate is in the future, subtract 1 from year
if (newDate > today)
newDate.setFullYear(newDate.getFullYear() - 1);
// Get the month and day value from newDate
var month = newDate.getMonth() + 1;
var day = newDate.getDate();
// Add the 0 padding to months and days smaller than 10
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
// Return a string in YYYY/MM/DD HH:MM format
return newDate.getFullYear() + '/' +
month + '/' +
day + ' ' +
newDate.getHours() + ':' +
newDate.getMinutes();
}
convertDate('2/27 6:57 PM'); // Returns: "2010/02/27 18:57"
convertDate('3/27 6:57 PM'); // Returns: "2009/03/27 18:57"
the code for adding THIS year is simple
var d = Date();
var withYear = d.getFullYear() + yourDate;
however, the logic behind considerating if it should be this year or last year could be harder to do
I would think this way: get today's date. If the date is higher than today's, it's last year, so add d.getFullYear()-1, otherwise add d.getFullYear()
This returns the current year:
var d = new Date();
var year = d.getFullYear();
To figure whether its this year or not you could just compare the day and month with the current day and month, and if necessary, subtract 1 from the year.
To get the day and month from the Date object:
d.getMonth(); // warning this is 0-indexed (0-11)
d.getDate(); // this is 1-indexed (1-31)