locked
between RRS feed

  • Question

  • i want to to do something like this   :                                                                                                 if  textbox1.text between 2012/01/01 and 2012/01/30 ,  textbox2.text = january

    what should i write for doing this in c# ?

    Thursday, November 29, 2012 6:51 PM

Answers

  • You can use the DateTime.TryParse or Parse methods (there are overloaded version too). The less than, greater than, equals, etc... operators were all overloaded so your code will be like this:


    //Define Constants for Beginning and End of Months
    
    DateTime compare;
    
    
    if(DateTime.TryParse(textbox1.Text, out compare)
    {
        if(compare >= BeginningOfJan && compare <= EndOfJan)
        {
            textbox2.Text = "January" //I would also define a constant or resource look up for culture info
        }
        else if....
    }


    However if would just be easier to do this:


    DateTime date = null;
    if(DateTime.TryParse(textbox1.Text, out date)
    {
        textbox2.Text = date.ToString("MMMM");
    }



    Thanks,


    Brad


    • Proposed as answer by servy42 Thursday, November 29, 2012 7:27 PM
    • Marked as answer by Bob ShenModerator Friday, December 7, 2012 9:34 AM
    Thursday, November 29, 2012 7:23 PM

All replies

  • You can use the DateTime.TryParse or Parse methods (there are overloaded version too). The less than, greater than, equals, etc... operators were all overloaded so your code will be like this:


    //Define Constants for Beginning and End of Months
    
    DateTime compare;
    
    
    if(DateTime.TryParse(textbox1.Text, out compare)
    {
        if(compare >= BeginningOfJan && compare <= EndOfJan)
        {
            textbox2.Text = "January" //I would also define a constant or resource look up for culture info
        }
        else if....
    }


    However if would just be easier to do this:


    DateTime date = null;
    if(DateTime.TryParse(textbox1.Text, out date)
    {
        textbox2.Text = date.ToString("MMMM");
    }



    Thanks,


    Brad


    • Proposed as answer by servy42 Thursday, November 29, 2012 7:27 PM
    • Marked as answer by Bob ShenModerator Friday, December 7, 2012 9:34 AM
    Thursday, November 29, 2012 7:23 PM
  • thank you , but i'm from iran and i want to work with persian months .
    Thursday, November 29, 2012 9:07 PM
  • Then why did you use "january" in your question? If only the names of the months differ, you can set them respectively. Otherwise, I don't know, but you might have to create a custom calendar...

    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    Thursday, November 29, 2012 9:19 PM
  • if you know please help me in codes instead of teaching LIVE lessons from Gandhi !
    Thursday, November 29, 2012 9:27 PM
  • Then you can use this version of ToString and pass in your culture information.

    Thanks,

    Brad

    Thursday, November 29, 2012 10:43 PM
  • thank you , but i'm from iran and i want to work with persian months .

    date.ToString("MMMM") should return the month name from the current CultureInfo.

    You can also use the DateTimeFormat's methods:

    textbox2.Text = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(date.Month);

    Friday, November 30, 2012 11:23 AM
  • Excuse me, I just though you should have said how you'd like the names of the months to appear in the question, because seemed a bit of a contradiction.

    I didn't know whether persian/iranian months have simply different names or whether they also have other different characteristics. From what I've seen here (don't know if it is accurate), they do, so this calendar shouldn't fit, as I suspected. As your can see @Brad Semrad and @Louis.fr have assumed it is OK, and therefore, given you answers that should be correct, but are not (because you didn't specify any of this and maybe expected that everyone knows about you're calendar).

    As such, I recommend you stop picking on my signature with its valuable lessons (translate them with Google Translator if you think otherwise), and start searching for a persian calendar (perhaps The Code Project has one, or Github, or some galleries out there) or build one on you're own (if you have any questions, I recommend you ask them in the C# forums, in a new thread).

    I hope I've made myself clear,


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    • Proposed as answer by JMCF125 Saturday, December 8, 2012 1:13 PM
    Friday, November 30, 2012 9:17 PM
  • thank you , but i'm from iran and i want to work with persian months .

    There is discussion about using a Persian calendar here: http://stackoverflow.com/questions/5001935/how-to-derive-custom-culture-from-net-cultureinfo-class
    • Proposed as answer by JMCF125 Saturday, December 8, 2012 1:13 PM
    Monday, December 3, 2012 3:56 PM
  • I disagree this should be marked as an answer, because persian months work differently (as Wikipedia shows), so this simply woudn't solve the problem.

    This shows that questions with little information may not be answered correctly, it's not @Brad Semrad's fault, we can't guess what's on the OP's mind. Yet, when I heard about persian months, I suspected they'd be different not just in the name (otherwise it would be quite simple, as the marked answer supposes), but that the calendar itself would be different.

    Therefore, I posted some useful advices (which the asker didn't seem to even consider, as he didn't replie or mark as answer), and @Louis.fr found a similar question on StackOverflow, probably going by unconsidered as well. As such, these two replies are the ones that should be marked as answers.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel

    Saturday, December 8, 2012 1:13 PM
  • @JMCF125: Grow up...

    Classified SR-2 | 2x Xeon W5580 - 3.20 GHz | 12x 2GB Kingston KHX2000C9D3T1K3/6GX | 2x MARS II/2DIS/3GD5 | SAMSUNG 830 MZ-7PC512D/AM 2.5" 512GB SATA III MLC | 4x Spinpoint F3EG HD503HI 500GB 5400 16MB SATA 3.0Gb/s |

    Saturday, December 8, 2012 4:57 PM
  • @VendorX, pardon me, but my concern is that people visiting this thread, perhaps with the same problem, wouldn't get the right answer.

    João Miguel


    • Edited by JMCF125 Saturday, December 8, 2012 6:54 PM shortening signature, the thread is already too big
    Saturday, December 8, 2012 6:54 PM