Date comparison with multilingual website

Ger Vang asked on January 9, 2015 23:20

I want to show a webpart during a certain month such as 2/1/2015 - 2/28/2015. I'm using this code in the visibility to show or hide the webpart:

ex: CurrentDate is = 1/9/2015

if(CurrentDate >= "2-1-2015" && CurrentDate <= "2-28-2015"){ return "true";}else{ return "false";}

The problem with this is that, if I switch to "Spanish", the date format changes from "mm/dd/yyyy" to "dd/mm/aaaa" for Spanish.

That makes a huge difference going from '1/9/2015' to '9/1/2015'. Is there a way for me to compare dates without worrying about how they are supposed to be formatted depending on the culture? I don't want to use the built in publish from (start date - end date).

Recent Answers

Roman Hutnyk answered on January 10, 2015 21:21

You are comparing DateTime to a string. Have you tried to create DateTime object instead of string and then compare it to a current date, which is also DateTime?

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.