Hi, I am trying to compare some dates. What I am trying to make, is a little macrescript that retrieves a date (DateHire) from the members and check if it is this employee anniversary, or if is going to happen within 14 days and if it happend maximum 14 days ago, but I cant get it to work!
My code:
@inherits umbraco.MacroEngines.DynamicNodeContext
@using System.Linq
@using umbraco.cms.businesslogic.member
@{
IEnumerable<umbraco.cms.businesslogic.member.Member > members = umbraco.cms.businesslogic.member.Member.GetAllAsList();
{
foreach (umbraco.cms.businesslogic.member.Member currentmembers in members)
{
string d = umbraco.library.FormatDateTime(currentmembers.getProperty("DateHire").Value.ToString(), "d/M");
var id = currentmembers.Id;
var image = currentmembers.getProperty("Image").Value.ToString();
var first = currentmembers.getProperty("NameFirst").Value.ToString();
var middle = currentmembers.getProperty("NameMiddle").Value.ToString();
var last = currentmembers.getProperty("NameLast").Value.ToString();
@*Profile image*@
if(String.IsNullOrEmpty(@image))
{
image = "/css/img/avatar.png";
}
else {
@* Use the thumbnail *@
if (image.IndexOf(".") != -1)
{
var uploadNamePath = image.Split('.');
var thumbnailNamePath = uploadNamePath[0] + "_thumb" + "." + uploadNamePath[1];
image = thumbnailNamePath;
}
}
int now = int.Parse(DateTime.Today.ToString("yyyy")); @*Todays date*@
var x = currentmembers.getProperty("DateHire").Value;
var y = string.Format("{0:yyyy}", @x); @*Date Of Hire *@
if(y.IsInt()==true)
{
var z = Convert.ToInt32(@y);
var years = now - z;
if (DateTime.Today.ToString("d/M") == d)
{
// Showing anniversary if it is today
}
else if (x < DateTime.Today.AddDays(14))
{
// Showing anniversary, if it going to happen within 14 days
}
else if (x > DateTime.Today.AddDays(14))
{
// Showing anniversary, if it has happend within 14 days
}
}
}
}
}
I hope you can understand what I am trying to do, just ask if not.
That sort of worked. Now I dont know how to make the if statement, to get the members from 14 days ago/after. I simply cannot figure out the logic
I tried DateTime.Today < dt but todays date will never be smaller than date of hire, because it has happend and it will always be greater than it to then.
Can you figure it out?
Again thanks for you help. I am very greatfull :-)
if (currentDateTime.Day == outAnniversary.Day)
{
// Showing anniversary if it is today
}
else if((currentDateTime - outAnniversary).TotalDays < 14 && (currentDateTime - outAnniversary).TotalDays > 0)
{
// Showing anniversary, if it going to happen within 14 days
}
else if((currentDateTime - outAnniversary).TotalDays > -14 && (currentDateTime - outAnniversary).TotalDays < 0)
{
// Showing anniversary, if it has happend within 14 days
}
}
First of all, sorry for the very late answer. I have not had a very nice christmas, because of a very bad breakup with my girlfriend. It really nocked me out.
I hope you still want to help me. I really appreciate it!
When I try using your code Charlie, it just outputs the raw code. I tried encapsulating it all with a @{}, but it did not work. Any advise?
I will mark it as solved, when it is solved, but unfortunatly there is still an error.
When saving the macroscript, I get this error:
Saving scripting file failed: c:\path-to-umbraco\MacroScripts\635567357154114802_ModuleAnniversary2.cshtml(8): error CS1526: A new expression requires (), [], or {} after type
I tried adding () a few places in line 8 (Umbraco.Core.Servic...), but no luck.
Again, thank you for your help. It is much appreciated :-)
What is it you are trying to do in this part?
The members does not have a property called anniversary. I calculate everything from the DateHire property. The other properties are rendered properly.
Thank you for your help. It is much appreciated! :-)
I am sorry to say that it was not in my original code, but in the code you posted. I am writing from my iPad, so I am not sure I can see the whole code, but it does not look like it is there.
Can I comment it out or is it important for the functionality? Then I will try that tomorrow :-)
I looked at you changes and it sort of works. The problem comes next year. Then the anniversary should show again, preferably with the number of years, the person has been hired.
Example:
This year, Daniel has 1 year anniversary.
How to compare dates? - u7
Hi, I am trying to compare some dates. What I am trying to make, is a little macrescript that retrieves a date (DateHire) from the members and check if it is this employee anniversary, or if is going to happen within 14 days and if it happend maximum 14 days ago, but I cant get it to work!
My code:
I hope you can understand what I am trying to do, just ask if not.
Thank you for your help!
Daniel
Hi Daniel,
Did you try to use Convert.ToDateTime method?
Thanks
Thank you!
That sort of worked. Now I dont know how to make the if statement, to get the members from 14 days ago/after. I simply cannot figure out the logic
I tried DateTime.Today < dt but todays date will never be smaller than date of hire, because it has happend and it will always be greater than it to then.
Can you figure it out?
Again thanks for you help. I am very greatfull :-)
Daniel
You are welcome Daniel,
Try to use :
Thanks,
Alexander
Thank you again.
Trouble is, that it is also comparing with the year. So it wont work :-/
Daniel
Daniel, Maybe, this will help you ?
Hi Daniel, What version of Umbraco are you using?
Charlie
Your code should be something like this :). I have done the dates down the bottom.
You have to use a method called GetDays(). This gives you you a number of days between the two dates and you can use that as a comparison.
Charlie :)
Umbraco.Core.Services.MemberService memberService = new Umbraco.Core.Services.MemberService; IEnumerable
if (currentDateTime.Day == outAnniversary.Day) { // Showing anniversary if it is today }
Happy new year guys!
First of all, sorry for the very late answer. I have not had a very nice christmas, because of a very bad breakup with my girlfriend. It really nocked me out.
I hope you still want to help me. I really appreciate it!
When I try using your code Charlie, it just outputs the raw code. I tried encapsulating it all with a @{}, but it did not work. Any advise?
Thank you again.
Daniel :-)
Hello,
Could you show me what is being rendered and i will help you out with the problem :).
Also you have a lot of conditional logic in your view which you should not have.
If
elseif
elseif
You should either use a switch statement or abstract it into a method in a class file and then call the method on your class :).
So write some code in c# compile it put it in the bin folder of you web site call the method and get the result :).
Charlie.
Hi, thank you for helping me! :-D
This is rendered:
Umbraco.Core.Services.MemberService memberService = new Umbraco.Core.Services.MemberService; IEnumerable members = memberService.GetAllMembers(); string hireDate; int memberId; IMedia media; string imageId = String.Empty; int outImageId; string imageUrl; string firstName; string middleName; string lastName; foreach (IMember currentMember in members) { if(currentMember.HasProperty("DateHire")) { hireDate = umbraco.library.FormatDateTime(currentMember.GetValue("DateHire").ToString(), "d/M"); } memberId = currentMember.Id; if(currentMember.HasProperty("DateHire")) { imageId = currentMember.GetValue("Image").ToString(); } if(currentMember.HasProperty("DateHire")) { firstName = currentMember.GetValue("NameFirst").ToString(); } if(currentMember.HasProperty("DateHire")) { middleName = currentMember.GetValue("NameMiddle").ToString(); } if(currentMember.HasProperty("DateHire")) { lastName = currentMember.GetValue("NameLast").ToString(); } if(String.IsNullOrEmpty(imageId)) { Int32.TryParse(imageId, out outImageId); if(outImageId > 0) { media = new Umbraco.Core.Services.MediaService(new RepositoryFactory()).GetById(outImageId); imageUrl = media!= null ? media.GetValue("umbracoFile").ToString() : "/css/img/avatar.png"; } } DateTime currentDateTime = DateTime.Now; DateTime outAnniversary = new DateTime(); string anniversary; if(currentMember.HasProperty("DateHire")) { hireDate = currentMember.GetValue("DateHire").ToString(); } if(currentMember.HasProperty("anniversary")) { anniversary = currentMember.GetValue("anniversary").ToString(); DateTime.TryParse(anniversary, out outAnniversary); } if (currentDateTime.Day == outAnniversary.Day) { Today!
} else if((currentDateTime - outAnniversary).TotalDays < 14 && (currentDateTime - outAnniversary).TotalDays > 0) { Going to happen
} else if((currentDateTime - outAnniversary).TotalDays > -14 && (currentDateTime - outAnniversary).TotalDays < 0) { Has happend
} }
Thank you! I owe you a cup of coffee, when this is done ;-)
Ha no worries all in a days work will look now :)
Are you using umbraco 7 btw? Is there any reason for @inherits umbraco.MacroEngines.DynamicNodeContext
@using System.Collections @using Umbraco.Core.Models @using Umbraco.Core.Persistence @inherits umbraco.MacroEngines.DynamicNodeContext @{
Going to happen
Has happend
If its ok can you mark me as solved please ;)
Thanks,
Charlie
Hi, thank you champ!
I will mark it as solved, when it is solved, but unfortunatly there is still an error.
When saving the macroscript, I get this error:
Saving scripting file failed: c:\path-to-umbraco\MacroScripts\635567357154114802_ModuleAnniversary2.cshtml(8): error CS1526: A new expression requires (), [], or {} after type
I tried adding () a few places in line 8 (Umbraco.Core.Servic...), but no luck.
Again, thank you for your help. It is much appreciated :-)
Its this line by the looks of it
Umbraco.Core.Services.MemberService memberService = new Umbraco.Core.Services.MemberService;
Should be
Umbraco.Core.Services.MemberService memberService = new Umbraco.Core.Services.MemberService();
Charlie :)
I already tried that and I get this error message:
Thank you again!
Daniel :-)
Hi,
Ok change this line:
Umbraco.Core.Services.MemberService memberService = new Umbraco.Core.Services.MemberService;
With this
IMemberService memberService = ApplicationContext.Current.Services.MemberService;
Success! It saved without errors.
I added some members with the hireDate of today, but they dont show up on the website. Nothing is rendered. :-/
I hope this will be the last thing :-)
Thank you again! :-)
Daniel
Hi Daniel,
You really need an IDE like Visual Studio http://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx
You will then be able to see some basic errors. Its hard with out debugging.
My suggestion would be to post the alias of the property you want to render.
On the doctype for the node there will be properties. Could you post the properties it has.
* INSERT In the code under the foreach() *
@currentMember.Id
Is this code being run on the correct page?
Charlie
Hi Charlie
I think i found the part, where it goes wrong.
This is being rendered:
What is it you are trying to do in this part? The members does not have a property called anniversary. I calculate everything from the DateHire property. The other properties are rendered properly.
Thank you for your help. It is much appreciated! :-)
Daniel
Hi Daniel sorry for the late reply been busy.
I was just following your code :')
What data are you trying to render?
What do you want to see on the front end?
Re:anniversary, you had this in your code at the beginning ;)
You had this in your code originally:
if(currentMember.HasProperty("anniversary")) { anniversary = currentMember.GetValue("anniversary").ToString(); DateTime.TryParse(anniversary, out outAnniversary); }
if (currentDateTime.Day == outAnniversary.Day) {
Today!
}Charlie :)
Hi :-)
I am sorry to say that it was not in my original code, but in the code you posted. I am writing from my iPad, so I am not sure I can see the whole code, but it does not look like it is there.
Can I comment it out or is it important for the functionality? Then I will try that tomorrow :-)
Thank you! :-)
Daniel
Looks like i have put it in there not sure why. Sorry about that :'). Let me know what you are trying to render and we will do it :). Nearly there :D
I tried to remove the before mentioned code and tried to correct the remaining code, and it failed miserably! :-)
It is this part, I am trying to get to work:
So we are back at my original question. How to compare dates, so that i can show todays anniversary and anniversaries which are +- 14 days :-)
Thank you for your help! :-)
Hi,
Could you tell me what the value of the following are:
@currentDateTime @outAnniversary @currentDateTime - outAnniversary.TotalDays
Can you also create a member with an anniversary that is with 14 days :). I think its not working because the conditions are not being met :).
Thanks,
Charlie :)
Hi,
I uploaded the website to this location: http://charlie.dlarsen.dk
You can login to umbraco with user: charlie and password: isawesome
Frontend login: akh and 1234
Then you can poke around the code and maybe find a solution. :-)
The result is shown to the right on every page.
The code is in the file: developer > scriptingfile > moduleanniversary2
Thank you for your help! You are awesome!
Daniel
Hi Charlie
I looked at you changes and it sort of works. The problem comes next year. Then the anniversary should show again, preferably with the number of years, the person has been hired.
Example: This year, Daniel has 1 year anniversary.
Next year, Daniel has 2 year anniversary.
Etc.
I hope it makes sense :-)
Thank you!
Daniel
is working on a reply...