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

G

Guest

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# ?
 
C

Chris, Master of All Things Insignificant

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
 
J

Jon Skeet [C# MVP]

PK9 said:
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.
 
G

Guest

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.AllowWhiteSpaces);

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


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

Guest

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.AllowWhiteSpaces);

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

Guest

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.AllowWhiteSpaces);

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

Guest

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.AllowWhiteSpaces);

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

Chris, Master of All Things Insignificant

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 said:
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.AllowWhiteSpaces);

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


Chris said:
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
 
G

Guest

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 said:
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 said:
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.AllowWhiteSpaces);

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


Chris said:
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


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# ?
 
G

Guest

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
 
C

Chris, Master of All Things Insignificant

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 said:
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 said:
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 said:
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.AllowWhiteSpaces);

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


:


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


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# ?
 
R

Richard L Rosenheim

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
 
G

Guest

This works... I was going into way too much detail with the ParseExact
function (format providers, culture info, etc).

Thanks!
 
G

Guest

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 said:
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 said:
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 said:
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


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.AllowWhiteSpaces);

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


:


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


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# ?
 
J

Jon Skeet [C# MVP]

PK9 said:
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.AllowWhiteSpaces);

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"));
}
}
 
R

Richard L Rosenheim

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

Glad it helped you,

Richard
 
C

Cor Ligthert

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
 

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

Top