I have a String called yourDate 2012-11-30. Now i need to compare this date with the system date to check if it has exceeded or equaled the system date.
How can i do this ?
My approach;
var tDay= Ext.Date.parse("2012-11-30", "Y-m-d");
var sDay= new Date().dateformat('Y-m-d'); // system date
I get an error message TypeError: (new Date).dateformat is not a function. How can i correct this ?
note: I am using Extjs
Try the following:
var sDay = Ext.Date.format(new Date(), 'Y-m-d')
But I don't think you need to format your current date for comparison.
You can just use var sDay = new Date() and then compare date objects like if(sDay > tDay)
Related
I am trying to display the date from time stamp using JavaScript but not working please check my code and it's not working due to string time but if i passed in number then it's working but this time coming from API so i must need to do something here. can anyone please help me.
var timestamp = '1607110465663'
var date = new Date(timestamp);
console.log(date.getTime())
You can convert string to number using methods such as Number or parseInt before creating Date object.
e.g.
var timestamp = '1607110465663'
var date = new Date(Number(timestamp));
console.log(date.getTime())
Parse string into int then...:
var timestamp = '1607110465663'
var date = new Date(parseInt(timestamp));
and use date object
console.log(date)
Remove the '' from timestamp
var timestamp = 1607110465663
var date = new Date(timestamp);
console.log(date.getTime())
console.log(date)
See JSfiddle
From the application, am capturing the date field in string format with the getText() function.I want to check if the captured date is less than the current date or not. Can some one pls help.
var actDate = locator.getText();
This gives me the output as "2021-04-23 01:11:10 GMT" .I will split the output to get only the date as "2021-04-13" .
var date=actDate.split(' ')[0];
After this, I wanted to compare with the current date.Am able to get the current date as :
this.todayDate= function(){
return moment(Date.now()).format('YYYY-MM-DD');
}
This gives me the output as 2021-04-23
Can someone help me how to compare these two values. I want to check the date returned from application is lesser the current date.
Compare the string directly. For e.g. "2021-04-13" < "2021-04-23"
This will return true.
Try this
var date = actDate.split(' ')[0]; // date = "2021-04-13"
var today = new Date().toISOString().slice(0, 10) // get current date
if(Date.parse(date) < new Date(today)){
//captured date is less than the current date
}
I am trying to turn my <c: out value into a Javascript date but when I try it I am constantly getting invalid date. This the string that I am trying to turn into a Javascript dateTime.
24-02-2021 17:34:27
I am getting this value by doing the following:
var d = ('<c:out value="${post.end}"/>');
And then I try changing it into a date by the following code:
var date1 = new Date(d);
console.log(date1);
And this is where I am getting the invalid date
Invalid Date
Now I'm not sure if this was because I have time at the end of the string, so I've also tried removing the time at the end by using substring to have the date string as:
24-02-2021
But yet this still has the same error. I have also replaced all the - with / so the date appears like this:
24/02/2021
What can I do to make sure that this date is a 'valid' date so I can use it within my code.
Your date string format is wrong. Please see the MDN reference for the Date built-in object.
You could do something like this:
d = d.split(' ')[0] // '24-02-2021'
const [day, month, year] = d.split('-')
const date1 = new Date(year, month, day)
I am trying to compare a couple of dates in javascript. First of it, I got froma database. In database it is in date format but after send it in a json file I guess it is just a string.
The second I got it in Date format in javascript. I found this script in javascript:
var todayAEJ = new Date();
var ddAEJ = todayAEJ.getDate();
var mmAEJ = todayAEJ.getMonth()+1; //January is 0!
var yyyyAEJ = todayAEJ.getFullYear();
if(ddAEJ<10){ddAEJ='0'+ddAEJ} if(mmAEJ<10){mmAEJ='0'+mmAEJ} todayAEJ = ddAEJ+'-'+mmAEJ+'-'+yyyyAEJ;
ANd it works like a charm.
The other date is like this: 13-01-2014
I tried to compare like this:
if(todayAEJ > val.date_End)...
But it returns true when today's day is bigger than val.date_End's day. So I cannot use this form when the month is diferent. What can I do in this case?
otherDate = '13-01-2014';
myDate=myDate.split("-");
var newDate=myDate[1]+","+myDate[0]+","+myDate[2];
otherDateTimeStamp = new Date(newDate).getTime();
todayAEJTimeStamp = new Date().getTime();
if(todayAEJTimeStamp > otherDateTimeStammp){
// your code
}
you can even use var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
Use above code...it will solve your problem!
Iinjoy...
If you can use MM-DD-YYYY format instead of DD-MM-YYYY you can use Date.parse:
var todayAEJTimeStamp = Date.parse(todayAEJ);//Date parse needs `MM-DD-YYYY` format
//if val.date_End is instance of Date
if(todayAEJTimeStamp > val.date_End.getTime()) {
//your code here
}
You have to compare them via microtime
var date = new Date("11/21/1987 16:00:00");
var milliseconds = date.getTime(); // Compare this milliseconds
Apart from timestamp solutions, I would recommend to use date.js in any case you want to play with Date object in JS. Date object has pretty odd behaviour in JS, and this library comes in handy.
I have astring directly coming form the database and I am creating object of Date as
Date dt=Date("23.03.2010") and it is comin NaN
whereas when I use Date dt= Date("03/23/2010") it works fine.
Any Idea how I can get this working?.
You can parse the string from the database and then create the date object. You will have to subtract 1 from the parsed month value to get a correct date.
var dateString = "23.03.2010";
var dateParts = dateString.split(".");
var dt = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);
You must pass string (parsed) dates in MDY format. This is to prevent ambiguity (does 5/6/2010 mean 6th May or 5th June?)
If you prefer, you can use new Date(year, month, day) format, and pass the arguments separately.
The safest way if is you can return the date as milliseconds since 1970-01-01, then you can easily create a Date object from it. Example:
var n = 1269302400000;
var dt = new Date(n);
Note that you'll want to invoke Date with the new operator - from the Mozilla Developer Center:
Invoking Date in a non-constructor
context (i.e., without the new
operator) will return a string
representing the current time.
The same page details the syntax of the Date constructor.
If you are constructing a Date from a string the format accepted is governed by the rules of the Date.parse method. See Microsoft's Date.parse documentation for a summary of these rules.
Give this a try...
var dateParts = '23.03.2010'.split('.');
// -1 from month because javascript months are 0-based
var dateObj = new Date(dateParts[2], dateParts[1]-1, dateParts[0]);
try
d="23.03.2010".split(".");
Date dt=Date([d[1],d[0],d[2]].join("/"))
i think it isn't the most beautiful way.