formatting datetime with all zeros at end

I have a vendor web services that i need to pass datetime like this : 2012-08-07T00:00:00

I tried DateTime.Parse("2012-08-07T00:00:00") but comes out as 2012/08/07 12 AM

It has to be exactly like that when I assign it to the datetime field. How can I do this? I thought maybe I should do DateTime.Now.Day + "T00:00:00" but couldnt get that working either
LVL 8
CamilliaAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
a date variable will always contain a date with the intrisic format (surely the US format). if you want a specific format, you need to store the result of your format into a string.
0
 
Éric MoreauSenior .Net ConsultantCommented:
have you tried:

dim d as string = date.today.tostring("s")
0
 
Julian HansenCommented:
C#
DateTime date1 = new DateTime(2012, 8, 21, 0, 0, 0);
Console.WriteLine(date1.ToString("s"));

Open in new window

VB.NET
Dim date1 As Date = #8/21/2008 0:00AM#
Console.WriteLine(date1.ToUniversalTime.ToString("s"))

Open in new window

"s" denotes sortable format - which is what you are looking for.

More info here http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#Y9120
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Tom BeckCommented:
C#
String.Format("{0:yyyy-MM-dd}T00:00:00", DateTime.Now.Date);
0
 
CamilliaAuthor Commented:
let me try, will post back
0
 
Éric MoreauSenior .Net ConsultantCommented:
be sure you keep the result of the ToString method in a string variable
0
 
CamilliaAuthor Commented:
i'm assigning it to a datetime field. But the answer is a string...how can i convert?

minDate =  String.Format("{0:yyyy-MM-dd}T00:00:00", DateTime.Now.Date)
mindate is datetime
0
 
CamilliaAuthor Commented:
not sure what you mean by that. Do you mean minDate should be changed to string? change the string to datetime?
0
 
CamilliaAuthor Commented:
i did this but doubt this is what u mean

minDate = DateTime.Parse(String.Format("{0:yyyy-MM-dd}T00:00:00", DateTime.Now.Date))
0
 
CamilliaAuthor Commented:
that didnt work either

minDate = DateTime.Parse(String.Format("{0:yyyy-MM-dd}T00:00:00", DateTime.Now.Date))

minDate is the vendor's web services paramter that's datetime. How can I change minDate??
0
 
CamilliaAuthor Commented:
0
 
CamilliaAuthor Commented:
no, datetime.parseexcat doesnt keep the format

 DateTime test2 = DateTime.ParseExact(test,
                                                         String.Format("{0:yyyy-MM-dd}T00:00:00", DateTime.Now.Date),
                                                         null);
0
 
Julian HansenCommented:
Did you try this

DateTime date1 = new DateTime(2012, 8, 21, 0, 0, 0);
DateTime test2 = date1.ToString("s");

Open in new window

0
 
CamilliaAuthor Commented:
this line wont work...cant convert string to datetime

DateTime test2 = date1.ToString("s");
0
 
Julian HansenCommented:
Sorry should be

String test2 = date1.ToString("s")
0
 
CamilliaAuthor Commented:
yes, but still, the vendor's web services has a "minDate" field that's datetime. How can I assign a string to datetime??
0
 
Tom BeckCommented:
EDIT:

How about this?

minDate = String.Format("{0:yyyy-MM-dd}T00:00:00", Convert.ToDateTime(<vendorsDatetime>.ToString()))
0
 
CamilliaAuthor Commented:
Mindate is datetime

String.Format("{0:yyyy-MM-dd}T00:00:00", Convert.ToDateTime(<vendorsDatetime>.ToString()))  is string

How can string be assigned to datetime?
0
 
Tom BeckCommented:
You will have to convert it back to datetime. As @emoreau said:
a date variable will always contain a date with the intrisic format (surely the US format). if you want a specific format, you need to store the result of your format into a string.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.