Answered by:
How to compare the CRM dates

Question
-
Dear All,
I have a doubt in comparing two CRMdatetime field,such that
consider,
Date1 CRMdatetime
date2 CRMdatetime
1) if both the days are same then a function is called
2)if date1 is next day of date2 and if its before 7pm then another function is called
3)if date1 is next day of date2 and if its after than 7pm then another function is called
i have given date1.date == date2.date and i can find if both does not fall on same day.but other two conditions is not possible
if i give date1.date > date2.date then error comes saying cannot use '>' in comparing two string
pl suggest me on this as i am new to CRM and its difficult to give this.
Monday, December 26, 2011 7:52 AM
Answers
-
hello krishnan
you cant directly compare CRM Datetime field
you got to first convert them first to DateTime Datatype and then use Compare method of DateTime type
Here is an example
DateTime date1 = Convert.ToDateTime(crmDateTime1.Value); DateTime date2 = Convert.ToDateTime(crmDateTime2.Value); int result = DateTime.Compare(date1, date2); string status = string.Empty; if (result < 0) status = "is earlier than"; // date1 < date2 else if (result == 0) status = "is the same time as"; // date1 = date2 else status = "is later than"; // date1 > date2
hthdkayif the response answered your question, please take a minute and mark the response as an answer.- Proposed as answer by Philippe LEAL Monday, January 2, 2012 10:19 AM
- Marked as answer by DavidJennawayMVP, Moderator Tuesday, January 3, 2012 10:06 AM
Monday, December 26, 2011 12:13 PM
All replies
-
HI Krishnan89
you can parse the dates as follows
if (DateTime.Parse(date1.date) < DateTime.Parse(date2.date))
for you case 2 for example you can validate as followsif (DateTime.Parse(date1.date).AddDays(1) == DateTime.Parse(date2.date) && DateTime.Parse(date1.date).Hour < 19 )
hope this helps.
Regards,
Damian SinayMonday, December 26, 2011 8:28 AM -
hello krishnan
you cant directly compare CRM Datetime field
you got to first convert them first to DateTime Datatype and then use Compare method of DateTime type
Here is an example
DateTime date1 = Convert.ToDateTime(crmDateTime1.Value); DateTime date2 = Convert.ToDateTime(crmDateTime2.Value); int result = DateTime.Compare(date1, date2); string status = string.Empty; if (result < 0) status = "is earlier than"; // date1 < date2 else if (result == 0) status = "is the same time as"; // date1 = date2 else status = "is later than"; // date1 > date2
hthdkayif the response answered your question, please take a minute and mark the response as an answer.- Proposed as answer by Philippe LEAL Monday, January 2, 2012 10:19 AM
- Marked as answer by DavidJennawayMVP, Moderator Tuesday, January 3, 2012 10:06 AM
Monday, December 26, 2011 12:13 PM -
Hi
Did this help?
Please make sure to mark as answer to the response that helped you get through. This will help others with similar problem identify the answer and also close this thread as resolved.
Thanks
DkayTuesday, December 27, 2011 6:46 AM -
Thanks to Crm_Developer.It worked fine.Thank u so much.
I have one more doubt
Consider i have a CRMDecimal a=444.3200000
I just want to omit the zero's and need a=444.32
Pl help me on this .If i try to convert it to money i cant.
Friday, December 30, 2011 12:48 PM -
Just to note that I marked an answer to the original question. The follow up question (of Friday, December 30, 2011 12:48 PM) is addressed on a separate thread - http://social.microsoft.com/Forums/en/crmdeployment/thread/70c552b1-31bd-42ec-bc7d-93d273683147
Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.ukTuesday, January 3, 2012 10:07 AMModerator