By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Converting Date Time Format

P: n/a
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#.
Oct 5 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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#.
Oct 5 '06 #2

P: n/a
"Alok yadav" <Al*******@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
>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");
Oct 5 '06 #3

P: n/a
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" <Al*******@discussions.microsoft.comescreveu na mensagem
news:2D**********************************@microsof t.com...
>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#.


Oct 5 '06 #4

P: n/a
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
Oct 6 '06 #5

P: n/a

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?

Oct 6 '06 #6

P: n/a
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
Oct 7 '06 #7

P: n/a
"Alok yadav" <Al*******@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
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!
Oct 7 '06 #8

P: n/a
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
Oct 9 '06 #9

P: n/a
"Flinky Wisty Pomm" <Pa********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr 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...
Oct 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.