?
Solved

formatting datetime with all zeros at end

Posted on 2012-08-21
19
Medium Priority
?
839 Views
Last Modified: 2012-08-21
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
0
Comment
Question by:Camillia
  • 10
  • 3
  • 3
  • +1
19 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38315906
have you tried:

dim d as string = date.today.tostring("s")
0
 
LVL 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 664 total points
ID: 38315930
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
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38315942
C#
String.Format("{0:yyyy-MM-dd}T00:00:00", DateTime.Now.Date);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:Camillia
ID: 38316068
let me try, will post back
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 38316089
be sure you keep the result of the ToString method in a string variable
0
 
LVL 7

Author Comment

by:Camillia
ID: 38316317
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
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 668 total points
ID: 38316363
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
 
LVL 7

Author Comment

by:Camillia
ID: 38316543
not sure what you mean by that. Do you mean minDate should be changed to string? change the string to datetime?
0
 
LVL 7

Author Comment

by:Camillia
ID: 38316550
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
 
LVL 7

Author Comment

by:Camillia
ID: 38316653
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
 
LVL 7

Author Comment

by:Camillia
ID: 38316712
0
 
LVL 7

Author Comment

by:Camillia
ID: 38316810
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
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38316857
Did you try this

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

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 38316887
this line wont work...cant convert string to datetime

DateTime test2 = date1.ToString("s");
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38316913
Sorry should be

String test2 = date1.ToString("s")
0
 
LVL 7

Author Comment

by:Camillia
ID: 38316946
yes, but still, the vendor's web services has a "minDate" field that's datetime. How can I assign a string to datetime??
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38316957
EDIT:

How about this?

minDate = String.Format("{0:yyyy-MM-dd}T00:00:00", Convert.ToDateTime(<vendorsDatetime>.ToString()))
0
 
LVL 7

Author Comment

by:Camillia
ID: 38317577
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
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 668 total points
ID: 38317793
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question