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 16 10337
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
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
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
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
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,
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
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
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
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
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 >> >> >>
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
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
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 >> >> >>
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: scott |
last post by:
I have a field with datetime values like below LISTING 1. Can someone help
me write code strip the time part so only values like "7/15/2005" will be
left.
Note - We must be able to strip dates...
|
by: rehughes |
last post by:
When I use: Format(datetime, "Short Time") or Format(datetime, "t") or
Format(datetime, "hh:mm") I always get an AM/PM value on the end of the time
string. How do I get a short time in military...
|
by: PK9 |
last post by:
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...
|
by: justinf |
last post by:
Hello! I'm looking for help on how to strip time data of my date string. The information in each field is extracted from another type database into access. For example:
12/31/2007 12:34:56:789...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |