how to compare dates using relational Operators in dart? - string

I'm working on a problem which requires to compare the date with other if the selected date is less than given then print Hello.
The date is present In String Format like given in Examaple
Ex:
if('2020-1-13'<'2021-1-5')
{
print(hello);
}

You should try this it should work
````
var date1 = DateTime.parse('2020-01-13').millisecondsSinceEpoch.toInt();
var date2 = DateTime.parse('2021-01-15').millisecondsSinceEpoch.toInt();
if(date1 < date2){
print('true');
}else{
print('false');
}
````

Instead of doing that just use the date's isBefore or isAfter operator
It would look something like the following:
final now = DateTime.now();
final yesterday = DateTime.now().subtract(Duration(days: 1));
print(now.isAfter(yesterday)); //true
Since your dates are in STring format, it you would have to use the DateTime.parse() method to construct your date objects, and then use the operators talked about above

package:basics provides DateTime extensions that can easily compare DateTime objects.
(Disclosure: I contributed those particular extensions.)

Related

Nodejs change time at date type after getting from Mongodb

I wanted to change time at date type which returning from mongodb with custom time like below
"2021-05-26T00:00:00.000Z"
to
"2021-05-26T10:20:00.000Z"
I wanted to change time from a variable at the date, so my technique was split this date with "T" then get time part and change it with custom time
let splitedTime = timev[0].validFrom.toString().split()[0];
let customTime = "10:20:00.000Z";
let finalTime = splitedTime + customTime;
but this split not working this giving me date like this "Wed May 26 2021 06:00:00 GM". Can you please help me for this?
Working with Date
Whilst I understand your logic of converting it to a string and then using string methods to convert it to your desired output, I believe a simpler approach is to use the Date object
function dateAdd(original, hours, minutes) {
const date = new Date(original);
date.setHours(original.getHours() + hours);
date.setMinutes(original.getMinutes() + minutes);
return date.toISOString();
}
When original = "2021-05-26T00:00:00.000Z" then the return value is "2021-05-26T10:20:00.000Z".
If you want a fixed time:
const date = new Date('2021-05-26T00:00:00.000Z');
date.setUTCHours(10);
date.setUTCMinutes(20);
date.setUTCSeconds(0);
date.setUTCMilliseconds(0);
// a cleaner approach:
date.setUTCHours(10, 20, 0); // hoursValue, minutesValue, secondsValue
console.log(date.toISOString());
Which produces the following:
"2021-05-26T10:20:00.000Z"
Another Solution
Your actual problem is being caused by the fact you call toString which returns a date string in the format of "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)" so when you're splitting by "T", that's way down at the end. toISOString will return the correct format.
Explanation
As you can see above, we avoid using string methods and use the methods that exist on Date. This approach is safer as you avoid issues with the difference between toISOString and toString. You may also find moment useful if you're using dynamic methods of changing dates regularly.
Note
In all honesty, I'm not entirely sure I understand the why behind what you're doing, so if I'm wrong please correct me so I can update my answer to be more relevant for you.
Learn More
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

How to compare date in linux for specific format

I want to compare date 08-08-2018 and 20-07-2018.
if (dt1 > $ dt2){
Success
}
Please help me.
The core
Time::Piece module
allows you to convert a date/time string to an object using the strptime class method. The resulting objects can be compared using the standard <, >, <=, >=, and == operators as you describe, and being a core module it is unlikely to need installing
Here's a short program that uses the values in your question, converts them to Time::Piece objects $dt1 and $dt2, and compares them as you describe
use strict;
use warnings 'all';
use feature 'say';
use Time::Piece;
my ($dt1, $dt2) = map { Time::Piece->strptime($_, '%d-%m-%Y') } qw/ 08-08-2018 20-07-2018 /;
if ( $dt1 > $dt2 ) {
say 'Success';
}
else {
say 'Failure';
}
output
Success
If your date formats are static, you have two easy options.
Option 1
Strip out the day, month, and year from each date.
Compare the two years.
If the years are the same, compare the two months.
If the months are the same, compare the two days.
Option 2
Strip out the day, month, and year from each date.
Create a new date string in the YYYY-MM-DD format
Do a standard string comparison on the two resulting strings.
I'm sorry, but I'm not in a position to provide you with sample code at this time.

Mongoose - Search dates stored as strings by date range

I have a mongo db with a paymentDate field stored as strings YYYY-MM-DD (I can't change how these are unfortunately), and I need to do a date range search on them.
I found a post suggesting something like this:
coll.find({paymentDate: new RegExp("^(2016-02-18|2016-06-19)", "i")});
But I can't seem to get that to work at all.
Any help is appreciated, I've hit a dead end here.
Storing dates as strings in YYYY-MM-DD format works fine in this case as the string ordering matches the date ordering so you can do:
coll.find({paymentDate: {$gte: "2016-02-18", $lte: "2016-06-19"}});
Your first objective should be changing the schema and use Date instead of String. But if it's really not possible and you can't think of a really complicated RegEx, you can do it with Javascript using $where, but it's performance will be much slower:
db.test.find({
$where: function () {
var docDate = new Date(this.paymentDate);
return (docDate >= new Date('2016-02-18') && docDate <= new Date('2016-02-19'))
}
})

Manipulation of date in Groovy language

I have this script in Groovy:
currentDate = new Date().format( 'yyyyMMdd' )
I want to be able to manipulate the date in order to 'play' with the dates of it..
for example if I have this:20150701 I want to subtract days, weeks or months for example if I subtract one day it will be 20150630.
How can I do it without using TimeCategory?
thanks!
Without TimeCategory, you can only add or subtract days. If you want to add/subtract other fields, TimeCategory is a good way to go.
If your annoyance with TimeCategory is the with syntax, one alternative would be to use mixins (although they are generally considered deprecated since traits have been added to Groovy):
[Date, Integer].each { it.mixin(groovy.time.TimeCategory) }
def lastMonth = new Date() - 1.months
OK I have the answer to it, I tried this and it worked:
currentDate = new Date()
def yesterday = currentDate - 1
currentDate = yesterday.format("yyyyMMdd")

DateTime Hex Format

I'm getting a hex string when converting a SPListItem's date.
eg. "0x01c9d2be|0x809a8800"
This question answers it.
However, what's the proper way to convert it, what format is the DateTime object in?
What if I want to set the datetime? Would I have to convert it into hex format and assign it as a hex string?
There has to be a better and ideal way to extract modify the DateTime object.
Any ideas?
Thanks.
I'm able to extract the date and time. That's fine.
So when you are modifying the DateTime of a ListItem, you just simply assign it as a DateTime format and it will interpret it correctly; no need to generate the hex that it returns.
A DateTime value in .NET can also be represented as a 64-bit value. The strange string format on DateTime fields on SharePoint list items is a variant of such a 64-bit value encoded as two 32-bit hex values. I have found no documentation from Microsoft on this format. But by trial and error I found the following conversion method:
string[] words = strVal.Split('|');
int high = int.Parse(words[0].Substring(2), System.Globalization.NumberStyles.HexNumber);
uint low = uint.Parse(words[1].Substring(2), System.Globalization.NumberStyles.HexNumber);
long ticks = high;
ticks = ticks << 32;
ticks += low;
DateTime t = new DateTime(ticks);
t = t.AddYears(1600);
where t holds the result.
Another solution can be found here. It has something to do how Office 2007 stores dates..
SPListItem.Properties DateTime Fields are in weird Hex format

Resources