|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Convert a date stringIn VB6, I can format a date string like the following:
format("12/15/06","yymmdd") and it returns 061215. In VB.Net, when I do the following: sdate.tostring("yymmdd"), it gave me an error "Value of type string cannot be converted to 'System.IFormatProvider' How can I do it in VB.NET ? Thanks. fpvt2,
I did not have any problems getting the following code to work: Dim d1 As DateTime = Now Console.WriteLine(d1.ToString("yyMMdd")) Maybe you can post the exact code that is giving you a problem? Kerry Moorman Show quoteHide quote "fp***@yahoo.com" wrote: > In VB6, I can format a date string like the following: > format("12/15/06","yymmdd") and it returns 061215. > In VB.Net, when I do the following: > sdate.tostring("yymmdd"), it gave me an error "Value of type string > cannot be converted to 'System.IFormatProvider' > > How can I do it in VB.NET ? > Thanks. > > I'm thinking he's using hungarian notation - so sdate is probably
declared as a string, not a datetime. Thanks, Seth Rowe Kerry Moorman wrote: Show quoteHide quote > fpvt2, > > I did not have any problems getting the following code to work: > > Dim d1 As DateTime = Now > > Console.WriteLine(d1.ToString("yyMMdd")) > > Maybe you can post the exact code that is giving you a problem? > > Kerry Moorman > > > "fp***@yahoo.com" wrote: > > > In VB6, I can format a date string like the following: > > format("12/15/06","yymmdd") and it returns 061215. > > In VB.Net, when I do the following: > > sdate.tostring("yymmdd"), it gave me an error "Value of type string > > cannot be converted to 'System.IFormatProvider' > > > > How can I do it in VB.NET ? > > Thanks. > > > > AFAIK the .ToString(...dateformat...) only works with a DateTime
object. Try this instead: MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd")) Thanks, Seth Rowe fp***@yahoo.com wrote: Show quoteHide quote > In VB6, I can format a date string like the following: > format("12/15/06","yymmdd") and it returns 061215. > In VB.Net, when I do the following: > sdate.tostring("yymmdd"), it gave me an error "Value of type string > cannot be converted to 'System.IFormatProvider' > > How can I do it in VB.NET ? > Thanks. "rowe_newsgroups" <rowe_em***@yahoo.com> ha scritto nel messaggio Pay attention with dates as "1/5/2006" where you can't tell if 1 is the day > AFAIK the .ToString(...dateformat...) only works with a DateTime > object. Try this instead: > > MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd")) or the month. Thank you, eveybody.
Convert.ToDateTime("12/15/06").ToString("yyMMdd")) does it. Thanks. Fabio Z wrote: Show quoteHide quote > "rowe_newsgroups" <rowe_em***@yahoo.com> ha scritto nel messaggio > > > AFAIK the .ToString(...dateformat...) only works with a DateTime > > object. Try this instead: > > > > MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd")) > > Pay attention with dates as "1/5/2006" where you can't tell if 1 is the day > or the month. fpvt,
Or in true VisualBasic CDate("12/15/06").ToString("yyMMdd") Cor <fp***@yahoo.com> schreef in bericht Show quoteHide quote news:1166200114.018476.103570@j72g2000cwa.googlegroups.com... > Thank you, eveybody. > Convert.ToDateTime("12/15/06").ToString("yyMMdd")) does it. > > Thanks. > > > Fabio Z wrote: >> "rowe_newsgroups" <rowe_em***@yahoo.com> ha scritto nel messaggio >> >> > AFAIK the .ToString(...dateformat...) only works with a DateTime >> > object. Try this instead: >> > >> > MessageBox.Show(Convert.ToDateTime("12/15/06").ToString("yyMMdd")) >> >> Pay attention with dates as "1/5/2006" where you can't tell if 1 is the >> day >> or the month. > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> ha scritto nel messaggio I repeat: watch out for locale dates.news:uEOE2WIIHHA.924@TK2MSFTNGP02.phx.gbl... > fpvt, > > Or in true VisualBasic > CDate("12/15/06").ToString("yyMMdd") > I don't think that the date "12/15/06" is hard coded. I.e. in my country (Italy) that date does not exists, because 15 would be the month (dd/mm/yyyy). Fabio,
Normally I am the one who is writting as you did in the dotNet newsgroup. I was missing the date completely., :-) CorShow quoteHide quote "Fabio" <znt.fa***@virgilio.it> schreef in bericht news:OFBpWPVIHHA.1064@TK2MSFTNGP04.phx.gbl... > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> ha scritto nel messaggio > news:uEOE2WIIHHA.924@TK2MSFTNGP02.phx.gbl... >> fpvt, >> >> Or in true VisualBasic >> CDate("12/15/06").ToString("yyMMdd") >> > > I repeat: watch out for locale dates. > I don't think that the date "12/15/06" is hard coded. > I.e. in my country (Italy) that date does not exists, because 15 would be > the month (dd/mm/yyyy). > > > fp***@yahoo.com wrote:
> In VB6, I can format a date string like the following: I guess you must convert the string to date first (something VB6 did> format("12/15/06","yymmdd") and it returns 061215. > In VB.Net, when I do the following: > sdate.tostring("yymmdd"), it gave me an error "Value of type string > cannot be converted to 'System.IFormatProvider' > > How can I do it in VB.NET ? for you, automagically), and *then* format the date the way you want. =) I mean: Dim D As String = _ Date.ParseExact(sDate, "mm/dd/yy", Nothing).ToString("yymmdd") HTH Regards, Branco. fp***@yahoo.com wrote:
> In VB6, I can format a date string like the following: That's because VB 'Proper's Evil Type Coersion implicitly changed the > format("12/15/06","yymmdd") and it returns 061215. > In VB.Net, when I do the following: > sdate.tostring("yymmdd"), it gave me an error "Value of type string > cannot be converted to 'System.IFormatProvider' string value "12/15/06" into a Date value and then formatted /that/. Visual Basic forces you to take more care over how things do things. Formatting (ToString'ing) a String is very different from the formatting a Date: ? DateTime.Parse( "12/15/06").ToString( "yymmdd" ) Or, since you start and end with Strings: sDate.Substring( 6, 2 ) _ & sDate.Substring( 0, 2 ) _ & sData.Substring( 3, 2 ) HTH, Phill W. |
|||||||||||||||||||||||