Converting Date Time Format

G

Guest

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

Flinky Wisty Pomm

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
}
 
M

Mark Rae

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");
 
B

Bruno Alexandre

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"
 
G

Guest

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
 
F

Flinky Wisty Pomm

Alok said:
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?
 
G

Guest

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
 
M

Mark Rae

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

Flinky Wisty Pomm

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?
 
M

Mark Rae

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top