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

Strip the "time" off from a string variable holding a datetime val

P: n/a
PK9
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.

What can I do to the string variable "strMyDate" to strip out the time portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0, using
the Format method. How can I accomplish this using C# ?
--
PK9
Jul 21 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a

I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

Note: I did this from memory and did not test it.
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for
display.

What can I do to the string variable "strMyDate" to strip out the time
portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0,
using
the Format method. How can I accomplish this using C# ?
--
PK9

Jul 21 '05 #2

P: n/a
PK9 <PK*@discussions.microsoft.com> wrote:
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.

What can I do to the string variable "strMyDate" to strip out the time portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0, using
the Format method. How can I accomplish this using C# ?


You need to use DateTime.ParseExact to convert the string to a
DateTime, and then DateTime.ToString specifying an appropriate format
specifier which only contains date parts.

If you could show us what you've already tried, we could help to fix
it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

P: n/a
PK9
Here is what I'm trying in order to get rid of the time portion of the date:

//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";

IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
I've tried different options, but can't get it to work correctly.

"Jon Skeet [C# MVP]" wrote:
PK9 <PK*@discussions.microsoft.com> wrote:
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.

What can I do to the string variable "strMyDate" to strip out the time portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0, using
the Format method. How can I accomplish this using C# ?


You need to use DateTime.ParseExact to convert the string to a
DateTime, and then DateTime.ToString specifying an appropriate format
specifier which only contains date parts.

If you could show us what you've already tried, we could help to fix
it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4

P: n/a
PK9
Chris,

Thanks, but I'm not trying to convert the string to a dateTime variable.
The end goal is to provide a string variable that only contains the month,
day and year. In your example below, the date "1/1/2005 12:00:00 AM" was the
date which was returned from the database. I want a string variable that
contains only "1/1/2005". Using C#, I'm trying to achieve this result, but I
haven't been successful. I provided my code in another reply, but here is
what I'm trying.
1) Convert the string to a datetime using ParseExact (I guess I have to do
this).
2) Convert the datetime to a string using toString() and stripping off the
time portion by providing a formatprovider.
My code is as follows:
//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";

IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
"Chris, Master of All Things Insignifican" wrote:

I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

Note: I did this from memory and did not test it.
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for
display.

What can I do to the string variable "strMyDate" to strip out the time
portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0,
using
the Format method. How can I accomplish this using C# ?
--
PK9


Jul 21 '05 #5

P: n/a
PK9
Chris,

Thanks, but I'm not trying to convert the string to a dateTime variable.
The end goal is to provide a string variable that only contains the month,
day and year. In your example below, the date "1/1/2005 12:00:00 AM" was the
date which was returned from the database. I want a string variable that
contains only "1/1/2005". Using C#, I'm trying to achieve this result, but I
haven't been successful. I provided my code in another reply, but here is
what I'm trying.
1) Convert the string to a datetime using ParseExact (I guess I have to do
this).
2) Convert the datetime to a string using toString() and stripping off the
time portion by providing a formatprovider.
My code is as follows:
//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";

IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
Jul 21 '05 #6

P: n/a
PK9
Chris,

Thanks, but I'm not trying to convert the string to a dateTime variable.
The end goal is to provide a string variable that only contains the month,
day and year. In your example below, the date "1/1/2005 12:00:00 AM" was the
date which was returned from the database. I want a string variable that
contains only "1/1/2005". Using C#, I'm trying to achieve this result, but I
haven't been successful. I provided my code in another reply, but here is
what I'm trying.
1) Convert the string to a datetime using ParseExact (I guess I have to do
this).
2) Convert the datetime to a string using toString() and stripping off the
time portion by providing a formatprovider.
My code is as follows:
//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";

IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
"Chris, Master of All Things Insignifican" wrote:

I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

Note: I did this from memory and did not test it.
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for
display.

What can I do to the string variable "strMyDate" to strip out the time
portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0,
using
the Format method. How can I accomplish this using C# ?
--
PK9


Jul 21 '05 #7

P: n/a
This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(Cstr(D.Date))

just put a CStr around the .Date method of the DateTime object. Easiest way
to get just the date portion of a datetime field
chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
Chris,

Thanks, but I'm not trying to convert the string to a dateTime variable.
The end goal is to provide a string variable that only contains the month,
day and year. In your example below, the date "1/1/2005 12:00:00 AM" was
the
date which was returned from the database. I want a string variable that
contains only "1/1/2005". Using C#, I'm trying to achieve this result,
but I
haven't been successful. I provided my code in another reply, but here is
what I'm trying.
1) Convert the string to a datetime using ParseExact (I guess I have to do
this).
2) Convert the datetime to a string using toString() and stripping off the
time portion by providing a formatprovider.
My code is as follows:
//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";

IFormatProvider format = new System.Globalization.CultureInfo("en-US",
true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
"Chris, Master of All Things Insignifican" wrote:

I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

Note: I did this from memory and did not test it.
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
>I have a string variable that holds the equivalent of a DateTime value.
>I
> pulled this datetime from the database and I want to strip off the time
> portion before displaying to the user.
>
> I am using C#
>
> eg.
> - String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM"
> from
> the database.
> - I do not care about the time portion, I only want "1/1/2005" for
> display.
>
> What can I do to the string variable "strMyDate" to strip out the time
> portion
>
> I've ran in circles using the DateTime.Parse and ParseExact functions,
> I'm
> not sure how to best go about this. It used to be so easy in VB 6.0,
> using
> the Format method. How can I accomplish this using C# ?
> --
> PK9


Jul 21 '05 #8

P: n/a
PK9
Unfortunately I don't believe that will work in C#. VB was always easier to
do conversions in my opinion because of their conversion functions "CDate,
CInt, etc". However, the CDate function doesn't apply to C#

"Chris, Master of All Things Insignifican" wrote:
This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(Cstr(D.Date))

just put a CStr around the .Date method of the DateTime object. Easiest way
to get just the date portion of a datetime field
chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
Chris,

Thanks, but I'm not trying to convert the string to a dateTime variable.
The end goal is to provide a string variable that only contains the month,
day and year. In your example below, the date "1/1/2005 12:00:00 AM" was
the
date which was returned from the database. I want a string variable that
contains only "1/1/2005". Using C#, I'm trying to achieve this result,
but I
haven't been successful. I provided my code in another reply, but here is
what I'm trying.
1) Convert the string to a datetime using ParseExact (I guess I have to do
this).
2) Convert the datetime to a string using toString() and stripping off the
time portion by providing a formatprovider.
My code is as follows:
//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";

IFormatProvider format = new System.Globalization.CultureInfo("en-US",
true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
"Chris, Master of All Things Insignifican" wrote:

I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(D.Date)

Note: I did this from memory and did not test it.
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
>I have a string variable that holds the equivalent of a DateTime value.
>I
> pulled this datetime from the database and I want to strip off the time
> portion before displaying to the user.
>
> I am using C#
>
> eg.
> - String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM"
> from
> the database.
> - I do not care about the time portion, I only want "1/1/2005" for
> display.
>
> What can I do to the string variable "strMyDate" to strip out the time
> portion
>
> I've ran in circles using the DateTime.Parse and ParseExact functions,
> I'm
> not sure how to best go about this. It used to be so easy in VB 6.0,
> using
> the Format method. How can I accomplish this using C# ?
> --
> PK9


Jul 21 '05 #9

P: n/a
PK9
Jon,
It looks like you're the only one who might be able to help me out with this
issue so far. If you have a chance to reply I would greatly appreciate it.

Thanks

"Jon Skeet [C# MVP]" wrote:
PK9 <PK*@discussions.microsoft.com> wrote:
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.

What can I do to the string variable "strMyDate" to strip out the time portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0, using
the Format method. How can I accomplish this using C# ?


You need to use DateTime.ParseExact to convert the string to a
DateTime, and then DateTime.ToString specifying an appropriate format
specifier which only contains date parts.

If you could show us what you've already tried, we could help to fix
it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #10

P: n/a
I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show((string)D.Date)

That doesn't work?
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
Unfortunately I don't believe that will work in C#. VB was always easier
to
do conversions in my opinion because of their conversion functions "CDate,
CInt, etc". However, the CDate function doesn't apply to C#

"Chris, Master of All Things Insignifican" wrote:
This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(Cstr(D.Date))

just put a CStr around the .Date method of the DateTime object. Easiest
way
to get just the date portion of a datetime field
chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
> Chris,
>
> Thanks, but I'm not trying to convert the string to a dateTime
> variable.
> The end goal is to provide a string variable that only contains the
> month,
> day and year. In your example below, the date "1/1/2005 12:00:00 AM"
> was
> the
> date which was returned from the database. I want a string variable
> that
> contains only "1/1/2005". Using C#, I'm trying to achieve this result,
> but I
> haven't been successful. I provided my code in another reply, but here
> is
> what I'm trying.
> 1) Convert the string to a datetime using ParseExact (I guess I have to
> do
> this).
> 2) Convert the datetime to a string using toString() and stripping off
> the
> time portion by providing a formatprovider.
> My code is as follows:
> //THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
> string strDate = "01/23/2005 12:00:00 AM";
>
> IFormatProvider format = new System.Globalization.CultureInfo("en-US",
> true);
> string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};
>
> DateTime NewDate = DateTime.ParseExact(DateWithTime,
> expectedFormats,
> format,
> System.Globalization.DateTimeStyles.AllowWhiteSpac es);
>
> string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
>
>
> "Chris, Master of All Things Insignifican" wrote:
>
>>
>> I think this is the C# code:
>> DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
>> MessageBox.Show(D.Date)
>>
>> This would be it in VB code (which is what normally work on)
>> Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
>> MessageBox.Show(D.Date)
>>
>> Note: I did this from memory and did not test it.
>> Chris
>>
>>
>> "PK9" <PK*@discussions.microsoft.com> wrote in message
>> news:EC**********************************@microsof t.com...
>> >I have a string variable that holds the equivalent of a DateTime
>> >value.
>> >I
>> > pulled this datetime from the database and I want to strip off the
>> > time
>> > portion before displaying to the user.
>> >
>> > I am using C#
>> >
>> > eg.
>> > - String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM"
>> > from
>> > the database.
>> > - I do not care about the time portion, I only want "1/1/2005" for
>> > display.
>> >
>> > What can I do to the string variable "strMyDate" to strip out the
>> > time
>> > portion
>> >
>> > I've ran in circles using the DateTime.Parse and ParseExact
>> > functions,
>> > I'm
>> > not sure how to best go about this. It used to be so easy in VB
>> > 6.0,
>> > using
>> > the Format method. How can I accomplish this using C# ?
>> > --
>> > PK9
>>
>>
>>


Jul 21 '05 #11

P: n/a
The syntax in VB.NET is:

Dim strMyDate As String
Dim dtMyDate As Date
Dim strOutput As String

dtMyDate = dtMyDate.Parse(strMyDate) ' convert from string to date
strOutput = dtMyDate.ToShortDateString
' or, you could do: strOutput = dtMyDate.ToString("MM-dd-yy")

The above is from memory, so sorry if there's an error. Shouldn't be too
different in C# (just add the appropriate semicolons and curly braces :-) )

Richard
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for display.
What can I do to the string variable "strMyDate" to strip out the time portion
I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0, using the Format method. How can I accomplish this using C# ?
--
PK9

Jul 21 '05 #12

P: n/a
PK9
This works... I was going into way too much detail with the ParseExact
function (format providers, culture info, etc).

Thanks!

"Richard L Rosenheim" wrote:
The syntax in VB.NET is:

Dim strMyDate As String
Dim dtMyDate As Date
Dim strOutput As String

dtMyDate = dtMyDate.Parse(strMyDate) ' convert from string to date
strOutput = dtMyDate.ToShortDateString
' or, you could do: strOutput = dtMyDate.ToString("MM-dd-yy")

The above is from memory, so sorry if there's an error. Shouldn't be too
different in C# (just add the appropriate semicolons and curly braces :-) )

Richard
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have a string variable that holds the equivalent of a DateTime value. I
pulled this datetime from the database and I want to strip off the time
portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from
the database.
- I do not care about the time portion, I only want "1/1/2005" for

display.

What can I do to the string variable "strMyDate" to strip out the time

portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm
not sure how to best go about this. It used to be so easy in VB 6.0,

using
the Format method. How can I accomplish this using C# ?
--
PK9


Jul 21 '05 #13

P: n/a
PK9
You can't convert the string to a DateTime like that. The cast is invalid.
I got it to work, you have to do a parse call on the DateTime structure.

i.e.
DateTime dtTemp;
dtTemp = DateTime.Parse(DateWithTime);
string strDateOutput = dtTemp.ToShortDateString();

Thanks for your help though.

Paul
"Chris, Master of All Things Insignifican" wrote:
I think this is the C# code:
DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
MessageBox.Show((string)D.Date)

That doesn't work?
Chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com...
Unfortunately I don't believe that will work in C#. VB was always easier
to
do conversions in my opinion because of their conversion functions "CDate,
CInt, etc". However, the CDate function doesn't apply to C#

"Chris, Master of All Things Insignifican" wrote:
This would be it in VB code (which is what normally work on)
Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
MessageBox.Show(Cstr(D.Date))

just put a CStr around the .Date method of the DateTime object. Easiest
way
to get just the date portion of a datetime field
chris
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
> Chris,
>
> Thanks, but I'm not trying to convert the string to a dateTime
> variable.
> The end goal is to provide a string variable that only contains the
> month,
> day and year. In your example below, the date "1/1/2005 12:00:00 AM"
> was
> the
> date which was returned from the database. I want a string variable
> that
> contains only "1/1/2005". Using C#, I'm trying to achieve this result,
> but I
> haven't been successful. I provided my code in another reply, but here
> is
> what I'm trying.
> 1) Convert the string to a datetime using ParseExact (I guess I have to
> do
> this).
> 2) Convert the datetime to a string using toString() and stripping off
> the
> time portion by providing a formatprovider.
> My code is as follows:
> //THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
> string strDate = "01/23/2005 12:00:00 AM";
>
> IFormatProvider format = new System.Globalization.CultureInfo("en-US",
> true);
> string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};
>
> DateTime NewDate = DateTime.ParseExact(DateWithTime,
> expectedFormats,
> format,
> System.Globalization.DateTimeStyles.AllowWhiteSpac es);
>
> string sDateWithoutTime = NewDate.ToString("M/d/yyyy");
>
>
> "Chris, Master of All Things Insignifican" wrote:
>
>>
>> I think this is the C# code:
>> DateTime D = (DateTime)("1/1/2005 12:00:00 AM")
>> MessageBox.Show(D.Date)
>>
>> This would be it in VB code (which is what normally work on)
>> Dim D as DateTime = CDate("1/1/2005 12:00:00 AM")
>> MessageBox.Show(D.Date)
>>
>> Note: I did this from memory and did not test it.
>> Chris
>>
>>
>> "PK9" <PK*@discussions.microsoft.com> wrote in message
>> news:EC**********************************@microsof t.com...
>> >I have a string variable that holds the equivalent of a DateTime
>> >value.
>> >I
>> > pulled this datetime from the database and I want to strip off the
>> > time
>> > portion before displaying to the user.
>> >
>> > I am using C#
>> >
>> > eg.
>> > - String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM"
>> > from
>> > the database.
>> > - I do not care about the time portion, I only want "1/1/2005" for
>> > display.
>> >
>> > What can I do to the string variable "strMyDate" to strip out the
>> > time
>> > portion
>> >
>> > I've ran in circles using the DateTime.Parse and ParseExact
>> > functions,
>> > I'm
>> > not sure how to best go about this. It used to be so easy in VB
>> > 6.0,
>> > using
>> > the Format method. How can I accomplish this using C# ?
>> > --
>> > PK9
>>
>>
>>


Jul 21 '05 #14

P: n/a
PK9 <PK*@discussions.microsoft.com> wrote:
Here is what I'm trying in order to get rid of the time portion of the date:

//THIS IS THE WAY THE DATE COMES BACK FROM THE DATABASE
string strDate = "01/23/2005 12:00:00 AM";
Out of interest, why isn't it stored as a DateTime in the database to
start with? That would make things much simpler.
IFormatProvider format = new System.Globalization.CultureInfo("en-US", true);
string[] expectedFormats = {"M/d/yyyy hh:mm:ss"};

DateTime NewDate = DateTime.ParseExact(DateWithTime,
expectedFormats,
format,
System.Globalization.DateTimeStyles.AllowWhiteSpac es);

string sDateWithoutTime = NewDate.ToString("M/d/yyyy");

I've tried different options, but can't get it to work correctly.


Your parsing format doesn't match the string you've given.

The following works fine:

using System;
using System.Globalization;

class Test
{
static void Main()
{
string fromDatabase = "01/23/2005 12:00:00 AM";

CultureInfo info = new CultureInfo ("en-US", true);

DateTime dt = DateTime.ParseExact (fromDatabase,
"MM/dd/yyyy hh:mm:ss tt",
info);

Console.WriteLine (dt.ToString ("MM/dd/yyyy"));
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #15

P: n/a
Over thinking a problem is something we are all probably guilty of at one
time or another.

Glad it helped you,

Richard
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:64**********************************@microsof t.com...
This works... I was going into way too much detail with the ParseExact
function (format providers, culture info, etc).

Thanks!

"Richard L Rosenheim" wrote:
The syntax in VB.NET is:

Dim strMyDate As String
Dim dtMyDate As Date
Dim strOutput As String

dtMyDate = dtMyDate.Parse(strMyDate) ' convert from string to date strOutput = dtMyDate.ToShortDateString
' or, you could do: strOutput = dtMyDate.ToString("MM-dd-yy")

The above is from memory, so sorry if there's an error. Shouldn't be too different in C# (just add the appropriate semicolons and curly braces :-) )
Richard
"PK9" <PK*@discussions.microsoft.com> wrote in message
news:EC**********************************@microsof t.com...
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user.

I am using C#

eg.
- String variable "strMyDate" holds the value "1/1/2005 12:00:00 AM" from the database.
- I do not care about the time portion, I only want "1/1/2005" for

display.

What can I do to the string variable "strMyDate" to strip out the time

portion

I've ran in circles using the DateTime.Parse and ParseExact functions, I'm not sure how to best go about this. It used to be so easy in VB 6.0,

using
the Format method. How can I accomplish this using C# ?
--
PK9


Jul 21 '05 #16

P: n/a
PK9

Are you sure the datetime is stored as a string.

In VSNet the date is presented as that what you show when you look to it.

However the datetime is normally stored as nanoseconds ticks after a date
somewhere in 17xx (what I always forget).

Therefore you can normally just use the datex.ToShortDateString to get your
wanted result.

I hope this helps?

Cor


Jul 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.