Click here to Skip to main content
13,046,176 members (83,614 online)
Rate this:
Please Sign up or sign in to vote.
See more:
DateTime date = DateTime.Now;
            string str="";
            date =Convert.ToDateTime(dtl.Rows[0]["createddate"].ToString());
            str = date.ToString("mm/dd/yyyy");

            DateTime datenow = DateTime.Now;
            string strnow="";
            strnow = datenow.ToString("mm/dd/yyyy");
            if (str != strnow)

//this gives me wrong month for both string variable why? and what is solution for that?
Posted 23-Nov-12 5:08am
Updated 23-Nov-12 6:08am
Krunal R 23-Nov-12 11:10am
what are you getting as output, show me..

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

In the string conversion of a date, lower case mm returns the minutes. For months, you must use upper case, MM. Your format string should therefore be "MM/dd/yyyy".

Happens to me, too.
sumit kausalye 23-Nov-12 11:41am
no even if i put lower case mm still its gives me wrong output..
only for mm,yyyy and dd is given me correct output,mm is wrong
RaisKazi 23-Nov-12 11:49am
Solution is suggesting u to use "MM/dd/yyyy". Upper case MM.
RaisKazi 23-Nov-12 11:48am
Correct, My 5.
ridoy 23-Nov-12 12:21pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100