Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Converting Date Time Format

Reply
Thread Tools

Converting Date Time Format

 
 
=?Utf-8?B?QWxvayB5YWRhdg==?=
Guest
Posts: n/a
 
      10-05-2006
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#.


 
Reply With Quote
 
 
 
 
Flinky Wisty Pomm
Guest
Posts: n/a
 
      10-05-2006
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#.


 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      10-05-2006
"Alok yadav" <> wrote in message
news:2D36FF23-81CC-4AA6-A90B-...

>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("d d/MM/yyyy");


 
Reply With Quote
 
Bruno Alexandre
Guest
Posts: n/a
 
      10-05-2006
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:2D36FF23-81CC-4AA6-A90B-...
>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#.
>
>



 
Reply With Quote
 
=?Utf-8?B?QWxvayB5YWRhdg==?=
Guest
Posts: n/a
 
      10-06-2006
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
 
Reply With Quote
 
Flinky Wisty Pomm
Guest
Posts: n/a
 
      10-06-2006

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?

 
Reply With Quote
 
=?Utf-8?B?QWxvayB5YWRhdg==?=
Guest
Posts: n/a
 
      10-07-2006
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
 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      10-07-2006
"Alok yadav" <> wrote in message
news:EC53F040-5837-4595-8627-...

> 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!


 
Reply With Quote
 
Flinky Wisty Pomm
Guest
Posts: n/a
 
      10-09-2006
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


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      10-10-2006
"Flinky Wisty Pomm" <> wrote in message
news: oups.com...

>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...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a time string to a more readable date, time Thierry Lam Python 0 10-27-2008 08:45 PM
covert time from date Hour min sec format to epoch time i.e time since 1 jan 1970 in C Summu82 C Programming 5 06-07-2006 02:51 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Date Format - best way of converting a string into a date format Brian Candy ASP .Net 2 02-18-2004 02:13 PM
Converting epoch time to "date/time" Marshall Barton NZ Computing 7 02-03-2004 04:29 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57