Converting Date Time Format

Discussion in 'ASP .Net' started by =?Utf-8?B?QWxvayB5YWRhdg==?=, Oct 5, 2006.

  1. i am using a webservice in which a method is serach. i use this method
    which accept a argument of date type in dd/MM/yyyy formate. i have a
    textbox which accept the date from the user, when i convert textbox data
    into Datatime formate it converted into MM/dd/yyyy formate, but i have a
    requirement in dd/MM/yyyy formate.

    please help me, i am using c#.
    =?Utf-8?B?QWxvayB5YWRhdg==?=, Oct 5, 2006
    #1
    1. Advertising

  2. You should always specify the format of a string rather than leaving it
    to the vagaries of any particular computer. Try

    try
    {
    DateTime.ParseExact(myDateString, "dd/MM/yyyy",
    CultureInfo.InvariantCulture);
    }
    catch(FormatException)
    {
    //oops, bad format
    }

    Alok yadav wrote:

    > i am using a webservice in which a method is serach. i use this method
    > which accept a argument of date type in dd/MM/yyyy formate. i have a
    > textbox which accept the date from the user, when i convert textbox data
    > into Datatime formate it converted into MM/dd/yyyy formate, but i have a
    > requirement in dd/MM/yyyy formate.
    >
    > please help me, i am using c#.
    Flinky Wisty Pomm, Oct 5, 2006
    #2
    1. Advertising

  3. =?Utf-8?B?QWxvayB5YWRhdg==?=

    Mark Rae Guest

    "Alok yadav" <> wrote in message
    news:...

    >i am using a webservice in which a method is serach. i use this method
    > which accept a argument of date type in dd/MM/yyyy formate. i have a
    > textbox which accept the date from the user, when i convert textbox data
    > into Datatime formate it converted into MM/dd/yyyy formate, but i have a
    > requirement in dd/MM/yyyy formate.
    >
    > please help me, i am using c#.


    Presumably, since you're using a webservice, your passing the date to the
    webservice as a string... e.g. "05/10/2006"

    Therefore, if your textbox contains "10/05/2006", all you need to do is:

    string strDateForWebService =
    Convert.ToDateTime(MyDateTextBox.Text).ToString("dd/MM/yyyy");
    Mark Rae, Oct 5, 2006
    #3
  4. you can build a function to convert that, I use this (I never saw an
    auomatic convertion on behalf of the framework, maybe someone knows)

    Use:

    Dim myNewDate as String = convertDateTypes("12/01/2006", "DMY")
    String myNewDate = convertDateTypes("12/01/2006", "DMY");

    Code in VB and C#:
    -------------------------------------------------------------------------
    ''' <summary>
    ''' converts a string with a date into diferent format
    ''' </summary>
    ''' <param name="sDate">Date to be converted</param>
    ''' <param name="sType">Convert date to... (MDY or DMY)</param>
    ''' <returns></returns>
    ''' <remarks></remarks>

    Function convertDateTypes(ByVal sDate As String, ByVal sType As String) As
    String
    ' MDY = mm/dd/yyyy
    ' DMY = mm/dd/yyyy
    Dim sResponse As String = ""
    Dim sSeparator As String = "/"
    If sType = "MDY" Then
    ' convert from DMY to MDY
    Dim sDay As String = sDate.Substring(0, 2)
    Dim sMon As String = sDate.Substring(3, 2)
    sResponse = sMon & sSeparator & sDay & sSeparator &
    sDate.Substring(sDate.Length - 4, 4)
    Else
    ' convert from MDY to DMY
    Dim sDay As String = sDate.Substring(3, 2)
    Dim sMon As String = sDate.Substring(0, 2)
    sResponse = sDay & sSeparator & sMon & sSeparator &
    sDate.Substring(sDate.Length - 4, 4)
    End If
    Return sResponse
    End Function

    -------------------------------------------------------------------------
    /// <summary>
    /// converts a string with a date into diferent format
    /// </summary>
    /// <param name="sDate">Date to be converted</param>
    /// <param name="sType">Convert date to... (MDY or DMY)</param>
    /// <returns></returns>

    string convertDateTypes(String sDate , String sType ) {
    // MDY = mm/dd/yyyy
    // DMY = mm/dd/yyyy
    string sResponse = "";
    string sSeparator = "/";
    if ( sType == "MDY" ) {
    // convert from DMY to MDY
    string sDay = sDate.Substring(0, 2);
    string sMon = sDate.Substring(3, 2);
    sResponse=sMon + sSeparator + sDay + sSeparator +
    sDate.Substring(sDate.Length-4, 4);
    }
    else {
    // convert from DMY to MDY
    string sDay = sDate.Substring(3, 2);
    string sMon = sDate.Substring(0, 2);
    sResponse=sMon + sSeparator + sDay + sSeparator +
    sDate.Substring(sDate.Length-4, 4);
    }
    return sResponse;
    }


    --

    Bruno Alexandre
    KĂžbenhavn, Danmark

    "a portuguese in Denmark"


    "Alok yadav" <> escreveu na mensagem
    news:...
    >i am using a webservice in which a method is serach. i use this method
    > which accept a argument of date type in dd/MM/yyyy formate. i have a
    > textbox which accept the date from the user, when i convert textbox data
    > into Datatime formate it converted into MM/dd/yyyy formate, but i have a
    > requirement in dd/MM/yyyy formate.
    >
    > please help me, i am using c#.
    >
    >
    Bruno Alexandre, Oct 5, 2006
    #4
  5. i want return date in datetime format of .net not in string. it returns in
    mm/dd/yyyy format by default. this methods return date in string format.
    thanks for u r help and time. but does not solve my problem i have to
    convert date in datetime and when i convert it, it gives in MM/dd/yyyy format
    =?Utf-8?B?QWxvayB5YWRhdg==?=, Oct 6, 2006
    #5
  6. Alok yadav wrote:
    > i want return date in datetime format of .net not in string. it returns in
    > mm/dd/yyyy format by default. this methods return date in string format.
    > thanks for u r help and time. but does not solve my problem i have to
    > convert date in datetime and when i convert it, it gives in MM/dd/yyyy format


    I'm not 100% I understand you. Here's how to do the conversion in
    either direction.

    public class TestDateStringConversion
    {
    // take a date time and return dd/mm/yyyy string
    public void TestDateToString()
    {
    DateTime myDateTime = DateTime.Now;
    string myString = myDateTime.ToString("dd/MM/yyyy");
    }


    // take a string in dd/mm/yyyy format and return a datetime
    public void TestStringToDate()
    {
    string myString = "06/10/2006";
    DateTime myDateTime = DateTime.ParseExact(myString, "dd/MM/yyyy",
    CultureInfo.InvariantCulture);
    }

    }


    Is there something else you need to do?
    Flinky Wisty Pomm, Oct 6, 2006
    #6
  7. i have tried u r code but no use it return in MM/dd/yyyy format

    string myString = "31/10/2006";
    DateTime myDateTime = DateTime.ParseExact(myString, "dd/MM/yyyy",
    CultureInfo.InvariantCulture);


    this method return date as 10/31/2006 this is MM/dd/yyyy format
    i want it in dd/MM/yyyy as 31/10/2006. but the result should be in datetime
    data type.
    if i use datetime.tostring("dd/MM/yyyy") then it returns strinng type date.
    but i want the return type be datetime object not string/

    i am using web service which take date in datetime format and in
    "dd/MM/yyyy" format. but in .Net the date type return or change or default
    format is "MM/dd/yyyy". so plz help me to find the solution. this phaze is
    final phase of my project and there i change ne thing like change in service
    provider then it effect my whole project so plz help me
    =?Utf-8?B?QWxvayB5YWRhdg==?=, Oct 7, 2006
    #7
  8. =?Utf-8?B?QWxvayB5YWRhdg==?=

    Mark Rae Guest

    "Alok yadav" <> wrote in message
    news:...

    > i want it in dd/MM/yyyy as 31/10/2006. but the result should be in
    > datetime
    > data type.


    ??? This makes no sense whatsoever! The DateTime variable will always hold
    its value internally the same way - no amount of formatting or culture
    manipulation will change that!

    > if i use datetime.tostring("dd/MM/yyyy") then it returns strinng type
    > date.


    Well of course it does! That's what .ToString() means... :)

    > but i want the return type be datetime object not string/


    So leave it alone!
    Mark Rae, Oct 7, 2006
    #8
  9. I think you're worrying about something that isn't a problem.

    To .Net,
    DateTime.ParseExact("10/31/2005", "MM/dd/yyyy") and
    Datetime.ParseExact("31/10/2005", dd/MM/yyyy") and
    DateTime.ParseExact("2005!31!10", "yyyy!dd!MM")

    are exactly the same value - a value of DateTime. I think what you're
    saying is that when you examine the date in Visual Studio, or write the
    date out, it still comes back as MM/dd/YYYY?

    Specify the format whenever you write or read a sate string and you'll
    have no problems. The date is not stored internally as a string, but as
    a great big long number. So long as the great big long number is
    correct, why does it matter what the default display mechanism is?


    Alok yadav wrote:

    > i have tried u r code but no use it return in MM/dd/yyyy format
    >
    > string myString = "31/10/2006";
    > DateTime myDateTime = DateTime.ParseExact(myString, "dd/MM/yyyy",
    > CultureInfo.InvariantCulture);
    >
    >
    > this method return date as 10/31/2006 this is MM/dd/yyyy format
    > i want it in dd/MM/yyyy as 31/10/2006. but the result should be in datetime
    > data type.
    > if i use datetime.tostring("dd/MM/yyyy") then it returns strinng type date.
    > but i want the return type be datetime object not string/
    >
    > i am using web service which take date in datetime format and in
    > "dd/MM/yyyy" format. but in .Net the date type return or change or default
    > format is "MM/dd/yyyy". so plz help me to find the solution. this phaze is
    > final phase of my project and there i change ne thing like change in service
    > provider then it effect my whole project so plz help me
    Flinky Wisty Pomm, Oct 9, 2006
    #9
  10. =?Utf-8?B?QWxvayB5YWRhdg==?=

    Mark Rae Guest

    "Flinky Wisty Pomm" <> wrote in message
    news:...

    >I think you're worrying about something that isn't a problem.


    Clearly.

    > The date is not stored internally as a string, but as
    > a great big long number. So long as the great big long number is
    > correct, why does it matter what the default display mechanism is?


    I think that's the concept that the OP can't quite get his head round...
    Mark Rae, Oct 10, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shaiboy_UK
    Replies:
    0
    Views:
    472
    Shaiboy_UK
    Oct 19, 2003
  2. Brian Candy
    Replies:
    2
    Views:
    1,149
    Janaka
    Feb 18, 2004
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  4. Summu82
    Replies:
    5
    Views:
    853
    Richard Heathfield
    Jun 7, 2006
  5. Thierry Lam
    Replies:
    0
    Views:
    286
    Thierry Lam
    Oct 27, 2008
Loading...

Share This Page