I have date from the date picker which I am accessing as -
var transdate = $j("input[name='enterdate']").val();
resulting in transdate = "6/22/2015"
I need to test if the entered date is between two dates which are defined as
startdate = '2015-02-01' and enddate = '2015-07-30'
How do I convert the transdate in yyyy-mm-dd format in the following code -
if ((new Date('transdate')>= startdate ) && (new Date('transdate') <= enddate )) {
alert("correct date entered");
}
Moment.js is a small handy library for dates that makes this easy.
moment('6/22/2015', 'M/D/YYYY')
.isBetween('2015-02-01', '2015-07-30'); // => true
Note that only the first (US format) date string needed an explicit format string supplied.
Moment can be useful for the parsing alone, eg. even if not using isBetween:
var transdate = moment('6/22/2015', 'M/D/YYYY').toDate();
var startdate = moment('2015-02-01').toDate();
var enddate = moment('2015-07-30').toDate();
transdate >= startdate && transdate <= enddate // => true
The string is not in the only format defined to be handled by the Date object. That means you have to parse it (with regular expressions or String#split or whatever), or use a library like MomentJS that will parse it for you. Once you've parsed the dates, you can compare them with < or >, etc.
Do not rely on Date to parse strings it's not defined to parse. You will run into implementations or locales where it doesn't work.
"6/22/2015" is trivial to parse with a regular expression:
var rex = /^(\d+)\/(\d+)\/(\d+)$/;
var match = rex.exec(transdate);
var dt = match ? new Date(+match[3], +match[1] - 1, +match[2]) : null;
That uses the Date constructor that accepts the parts of the date as individual numeric arguments (year, month, day). The + converts strings to numbers. The [x] are capture groups from the regex. You have to subtract one from the month because months start with 0 in JavaScript.
Similar questions have been asked many, many times but I can't seem to find a duplicate. Given the unreliability of the Date constructor to parse strings, the simplest solution is to parse the string yourself:
function parseMDY(s) {
var b = s.split(/\D/);
return new Date(b[2], b[0]-1, b[1]);
}
Here is the JSFIDDLE of you output.
Moment.js will give you good flexibility in coding.
Dont forget to add jquery and moment.js in your html
var transdate="6/22/2014";
var convertStringToValidDate = new Date(transdate);
$(document).ready(function(){
$("#selectedDate").text(transdate);
$("#validDate").text(convertStringToValidDate);
converttoformat = moment(convertStringToValidDate).format("YYYY-MM-DD");
$("#converttoyyyymmdd").text(converttoformat);
if(moment(converttoformat).isBetween('2015-02-01', '2015-07-30')){
$("#result").text("Date lies in between");
}
else{
$("#result").text("Date is out of scope");
}
});
Related
I have this function that determines if two sets of dates are overlapping. When the set of dates are in the same year, it works correctly and returns true if there's overlap. If one of the dates is from a previous year, the comparison fails to detect the overlap.
For instance:
var obj = { startDate1: "02/01/2020",
endDate1: "03/01/2020",
startDate2:"02/05/2020",
endDate2:"02/15/2020" }
Using:
if ((obj.endDate1 >= obj.startDate2 && obj.startDate1 <= obj.endDate2) ||
(obj.endDate2 >= obj.startDate1 && obj.startDate2 <= obj.endDate1)) {
}
Returns true since the dates overlap.
However, when startDate1 is in a different year (2019), like so:
var obj = { startDate1: "12/01/2019",
endDate1: "03/01/2020",
startDate2:"02/05/2020",
endDate2:"02/15/2020" }
The expression above fails to detect the overlap even though startDate1 and endDate1 do in fact overlap with startDate2 and endDate2.
I've tried using moment and regex to format the dates, but to no avail. Any ideas what I'm doing wrong?
Edit: Here's how I'm using moment:
const obj = {
startDate1: moment(new Date(value.startDate)).format("MM/DD/YYYY"),
endDate1: moment(new Date(value.endDate)).format("MM/DD/YYYY"),
startDate2: moment(new Date(startDate)).format("MM/DD/YYYY"),
endDate2: moment(new Date(endDate)).format("MM/DD/YYYY")
};
The values I'm passing in to new Date are iso date strings.
Sorry for the confusion..
Edit #2/Answer:
I just converted the dates using native JS Date.parse.
const obj = {
certStart: Date.parse(value.startDate),
certEnd: Date.parse(value.endDate),
startDate: Date.parse(startDate),
endDate: Date.parse(endDate) };
Using the Native Date function in js
var obj = {
startDate1: "02/01/2020",
endDate1: "03/01/2020",
startDate2:"02/05/2020",
endDate2:"02/15/2020"
};
// Create a clone of obj object and convert values to timestamp.
var $dates = {};
$dates.startDate1 = new Date(obj.startDate1).getTime();
$dates.startDate2 = new Date(obj.startDate2).getTime();
$dates.endDate1 = new Date(obj.endDate1).getTime();
$dates.endDate2 = new Date(obj.endDate2).getTime();
Comparing time:
if (($dates.endDate1 >= $dates.startDate2 && $dates.startDate1 <= $dates.endDate2) ||
($dates.endDate2 >= $dates.startDate1 && $dates.startDate2 <= $dates.endDate1)) {
// etc...
}
Use new Date() or moment.js to create and manipule dates.
var obj = { startDate1: moment("12/01/2019"),
endDate1: moment("03/01/2020"),
startDate2:moment("02/05/2020"),
endDate2:moment("02/15/2020")}
and moment functions to manipulate dates
Despite your tag, it doesn't seem like you are actually formatting the dates using momentjs - unless you aren't sharing some of the code. It really looks like you are making string comparisons. For date comparison in general, I suggest reading the related: Compare two dates with JavaScript
Relevant MDN on Date: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
And don't forget to reference the moment.js docs: https://momentjs.com/docs/
EDIT: As already pointed out, you are using .format(), which returns a display string https://momentjs.com/docs/#/displaying/format/ - so you're still comparing strings instead of dates.
You may want to checkout isBetween: https://momentjs.com/docs/#/query/is-between/
Parse date string to Date, then get its value as a number:
const [m, d, y] = dateStr.split('/').map(Number)
// month is 0-indexed, whereas year and day are 1-indexed
const dateVal = new Date(y, m - 1, d).valueOf()
Then, you can easily compare them with >, <, and so on.
If the date string is in YYYY/MM/DD format then the string comparison will work.
var obj = {
startDate1: "2019/12/01",
endDate1: "2020/03/01",
startDate2: "2020/02/05",
endDate2: "2020/02/15"
}
// this will work
if ((obj.endDate1 >= obj.startDate2 && obj.startDate1 <= obj.endDate2) ||
(obj.endDate2 >= obj.startDate1 && obj.startDate2 <= obj.endDate1)) {
console.log('overlap detected')
}
I have to parse a date and time string of format "2015-01-16 22:15:00". I want to parse this into JavaScript Date Object. Any help on this?
I tried some jquery plugins, moment.js, date.js, xdate.js. Still no luck.
With moment.js you can create a moment object using the String+Format constructor:
var momentDate = moment('2015-01-16 22:15:00', 'YYYY-MM-DD HH:mm:ss');
Then, you can convert it to JavaScript Date Object using toDate() method:
var jsDate = momentDate.toDate();
A better solution, I am now using date.js - https://code.google.com/p/datejs/
I included the script in my html page as this -
<script type="text/javascript" src="path/to/date.js"></script>
Then I simply parsed the date string "2015-01-16 22:15:00" with specifying the format as,
var dateString = "2015-01-16 22:15:00";
var date = Date.parse(dateString, "yyyy-MM-dd HH:mm:ss");
new Date("2015-01-16T22:15:00")
See Date.parse().
The string must be in the ISO-8601 format. If you want to parse other formats use moment.js.
moment("2015-01-16 22:15:00").toDate();
I was trying to use moment.js guys. But since I was having this error, "ReferenceError: moment is not defined", I had to skip it for now. I am using an temporary workaround for now.
function parseDate(dateString) {
var dateTime = dateString.split(" ");
var dateOnly = dateTime[0];
var timeOnly = dateTime[1];
var temp = dateOnly + "T" + timeOnly;
return new Date(temp);
}
Bit annoying, but not hard to write a parsing method yourself without any dependencies. Regular expressions are great to check the input against one or many date formats. Though, the Date in the format provided 'just works' by now. I.e. new Date('2015-01-16 22:15:00') works for me in firefox. I did this for a date that looked like '08.10.2020 10:40:32', which does not work and maybe the date provided does not work in some browsers. But this way you can parse it without relying on built in parsing methods.
function getAsDate(input) {
if (!input) return null;
if (input instanceof Date) return input;
// match '2015-01-16 22:15:00'
const regexDateMatch = '^[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}$';
if(input.match(regexDateMatch)) {
const [year, month, day, hours, minutes, seconds] = input.split(/[-: ]/).map(x => parseInt(x));
const date = new Date(year, month, day, hours, minutes, seconds);
return date;
}
// Date formats supported by JS
return new Date(input);
}
Using Regex capture groups as suggested would also be an option.
function getAsDate(input) {
if (!input) return null;
if (input instanceof Date) return input;
// match '2015-01-16 22:15:00'
const regexDateMatch = '^([0-9]{4})\-([0-9]{1,2})\-([0-9]{1,2})\ ([0-9]{2})\:([0-9]{2})\:([0-9]{2})$';
let match;
if((match = input.match(regexDateMatch))) {
const [year, month, day, hours, minutes, seconds] = match.slice(1, 7).map(x => parseInt(x));
const date = new Date(year, month, day, hours, minutes, seconds);
return date;
}
// Date formats supported by JS
return new Date(input);
}
Hopefully JS upcomming temporal API will have a proper widely supported way to parse dates with a custom template. Then eventually we won´t have to deal with this anymore and all the other oddities of JS Date.
If you are sure it's in the desired format and don't need to error check, you can parse it manually using split (and optionally replace). I needed to do something similar in my project (MM/DD/YYYY HH:mm:ss:sss) and modified my solution to fit your format.
Notice the subtraction of 1 in the month.
var str = "2015-01-16 22:15:00";
//Replace dashes and spaces with : and then split on :
var strDate = str.replace(/-/g,":").replace(/ /g,":").split(":");
var aDate = new Date(strDate[0], strDate[1]-1, strDate[2], strDate[3], strDate[4], strDate[5]) ;
I need to subtract a date like 1/26/2015 from a date-time like 2016-01-27T01:10:57.569000+00:00. From what I've read converting both to distance in milliseconds from Epoch and then subtracting is the easiest way. I've tried using various methods, but all the methods seem to say 2016-01-27T01:10:57.569000+00:00 is invalid data. The method .getTime() works great for the 1/26/2015 format, but it can't read the 2016-01-27T01:10:57.569000+00:00.
How does one go about getting the date/time UTC time into milliseconds?
On a complicated way you can use a regex to extract each part of the date as string and then use them in a new Date with all parameters:
function getTimeDifference(){
var regEx = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):([\d.]+)/;
var dateString = '2016-01-27T01:10:57.569000+00:00';
var r = regEx.exec( dateString );
var date1 = new Date(r[1], r[2]-1, r[3], r[4], r[5], r[6]); // Notice the -1 in the month
var date2 = new Date('1/26/2015');
var difference = date1 - date2;
Logger.log(difference);
}
I ended up using this. When I call parseDate(), I used getTime() to get the date in milliseconds then subtracted them and converted them to days. For my use case the time didn't have to be down to the second, but if it did, it wouldn't be hard to parse more info from the string. I ran into trouble initially because as a beginner Javascript writer I didn't know why apps script wouldn't accept this format into the date constructor.
function parseDate(str) {
//This should accept 'YYYY-MM-DD' OR '2016-01-27T01:10:57.569000+00:00'
if(str.length == 10){
var mdy = str.split('-');
return new Date(mdy[0], mdy[1]-1, mdy[2]);
}
else
{
var mdy = str.split('-');
var time = mdy[2].split('T');
var hms = time[1].split(':');
return new Date(mdy[0], mdy[1]-1, time[0], hms[0], hms [1]);
}
}
If you are confident that the values in the date strings will always be valid and that the ISO8601 string will always have offset 00:00 (i.e. UTC), then simple parse functions are:
// Parse ISO 8601 format 2016-01-27T01:10:57.569000+00:00
function parseISOUTC(s) {
var b = s.split(/\D/);
return new Date(Date.UTC(b[0],b[1]-1,b[2],b[3],b[4],b[5],b[6]));
}
document.write(parseISOUTC('2016-02-04T00:00:00.000+00:00'));
// Parse US format m/d/y
function parseMDY(s) {
var b = s.split(/\D/);
return new Date(b[2],b[0]-1,b[1]);
}
document.write('<br>'+ parseMDY('2/4/2016'))
document.write('<br>'+ (parseISOUTC('2016-02-04T00:00:00.000+00:00') - parseMDY('2/4/2016')))
Note that the first string is UTC and the second will be treated as local (per ECMAScript 2015), so the difference between 2016-02-04T00:00:00.000+00:00 and 2/4/2016 will be the time zone offset of the host system.
I am facing some problem in comparing dates in javascript.
I have dates in "1-Dec-2014" & "19-Nov-2014" format.
While I compared the dates like
var stDate='19-Nov-2014';
var endDate='1-Dec-2014';
if(stDate < endDate){
console.log('Hi');
}else{
console.log('Bye');
}
In out put it shows me "Bye", but it should be "Hi".
What I observed it this comparison compares the date (initial argument) in respective dates.
I am very new to javascript . I am not getting any way to solve this .
Please help me.
Currently you are only comparing two strings. You should compare the dates like this:
new Date(stDate) < new Date(endDate)
var stDate='19-Nov-2014';
var endDate='1-Dec-2014';
if(new Date(stDate) < new Date(endDate)){
console.log('Hi');
}else{
console.log('Bye');
}
Just like #Arun P Johnny said, you are comparing Strings instead of actual dates. You need to convert you date to Date objects before comparing them. Check this out.
As noted in other answers, you need to convert the strings to Date objects. The best way to do that is to parse the strings using a function like:
/* Return a Date object or NaN given a string in d, MMM y or dd-MMM-yyyy format
** e.g. 5 Dec, 2014
** Avoid conversion of two digit dates to 20th century
** Returns NaN if string is not a valid date
*/
function parseDMMMY(s) {
var b = s.match(/\w+/g);
var months = {jan:0, feb:1, mar:2, apr:3, may:4, jun:5,
jul:6, aug:7, sep:8, oct:9, nov:10, dec:11};
if (b) {
var d = new Date();
d.setHours(0,0,0,0);
d.setFullYear(b[2], months[b[1].toLowerCase()], b[0]);
}
return b && d.getFullYear() == b[2] && d.getDate() == b[0]? d : NaN;
}
This also treats dates like 1-1-19 as in year 19, rather than 1919 which may happen if the values are passed directly to the Date constructor rather than using the set* methods.
Now you can do:
var stDate = parseDMMMY('19-Nov-2014');
var endDate = parseDMMMY('1-Dec-2014');
To handle dates correctly i'll quote Pavel Hodek
The best you can do is use the ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS
For example:
new Date('2011-04-11')
or
new Date('2011-04-11T11:51:00')
For more Info: MDN | Date
Edit:
For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11', '11', '51', '00')
Note that the number of the month must be 1 less.
Important note:
The "ISO format" solution doesn't work 100% time. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). Calling toString returns the local time therefore depending on the users timezone in some cases new Date('2011-04-11') will give you 2011-04-10.
Chrome behaves the same as Internet Explorer 9 and Firefox behaves the same as Internet Explorer 10+.
Safe solution is passing string value with Z to be parsed as UTC value e.g. new Date('2011-04-11T10:20:30Z'). Best practice should always be to store dates as UTC and make computations as UTC. Only for presentation they should be presented as local time.
Once you have both dates (after parsing and/or using constructor) you can safely compare them.
function ValidateDate() {
var SDate = document.getElementById('<%=sdate.ClientID%>').value;
var EDate = document.getElementById('<%=edate.ClientID%>').value;
var sdate = new Date(stDate);
var Edate = new Date(endDate);
var alertReason1 = 'To Date must be greater than or equal to From Date.'
//var endDate = new Date(EDate);
//var startDate = new Date(SDate);
if (SDate != "" && EDate != "" && Edate > sdate ) {
alert(alertReason1);
return false;
}
}
I have a form input with an id of 'date_trans'. The format for that date input (which is validated server side) can be any of:
dd/mm/yyyy
dd-mm-yyyy
yyyy-mm-dd
yyyy/mm/dd
However, before posting the form, I'd like to check if the date_trans field has a date that is equal to today's date. Its ok if the date taken is the client's date (i.e. it uses js), since I run a double check on the server as well.
I'm totally lost on how to do the date comparrison in jQuery or just plain old javascript. If it helps, I am using the jquery datepicker
A simple date comparison in pure JS should be sufficient:
// Create date from input value
var inputDate = new Date("11/21/2011");
// Get today's date
var todaysDate = new Date();
// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
// Date equals today's date
}
Here's a working JSFiddle.
for completeness, taken from this solution:
You could use toDateString:
var today = new Date();
var isToday = (today.toDateString() == otherDate.toDateString());
no library dependencies, and looking cleaner than the 'setHours()' approach shown in a previous answer, imho
Try using moment.js
moment('dd/mm/yyyy').isSame(Date.now(), 'day');
You can replace 'day' string with 'year, month, minute' if you want.
function sameDay( d1, d2 ){
return d1.getUTCFullYear() == d2.getUTCFullYear() &&
d1.getUTCMonth() == d2.getUTCMonth() &&
d1.getUTCDate() == d2.getUTCDate();
}
if (sameDay( new Date(userString), new Date)){
// ...
}
Using the UTC* methods ensures that two equivalent days in different timezones matching the same global day are the same. (Not necessary if you're parsing both dates directly, but a good thing to think about.)
Just use the following code in your javaScript:
if(new Date(hireDate).getTime() > new Date().getTime())
{
//Date greater than today's date
}
Change the condition according to your requirement.Here is one link for comparision compare in java script
The following solution compares the timestamp integer divided by the values of hours, minutes, seconds, millis.
var reducedToDay = function(date){return ~~(date.getTime()/(1000*60*60*24));};
return reducedToDay(date1) == reducedToDay(date2)
The tilde truncs the division result (see this article about integer division)
Date.js is a handy library for manipulating and formatting dates. It can help in this situation.
Try this
// method to check date is less than today date
isLessDate(schedule_date : any){
var _schedule_date = new Date(schedule_date);
var date = new Date();
var transformDate = this.datePipe.transform(date, 'yyyy-MM-dd');
var _today_date = new Date(''+transformDate);
if(_schedule_date < _today_date){
return 'small'
}
else if(_schedule_date > _today_date){
return 'big'
}
else {
return 'same'
}
}
The Best way and recommended way of comparing date in typescript is:
var today = new Date().getTime();
var reqDateVar = new Date(somedate).getTime();
if(today === reqDateVar){
// NOW
} else {
// Some other time
}
TodayDate = new Date();
if (TodayDate > AnotherDate) {} else{}
< = also works, Although with =, it might have to match the milliseconds.
There is a simpler solution
if (inputDate.getDate() === todayDate.getDate()) {
// do stuff
}
like that you don't loose the time attached to inputDate if any