473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem converting a string into date in vb.net

198 100+
Hi
I have a probleum in vconverting a string value into date in vb.net
e.g
str="31.12.2005"
i want to convert it into date format like "mm/dd/yyyy"
i use CDATE function for it but every time error comes " String can't be converted into date"

but when i use any other value like 12.31.2005 it works fine

how can i change the first str value into 2nd value because from where i am accessing the always have the value "31.12.2006"


please give me some idea
Jun 3 '08 #1
10 17603
CyberSoftHari
487 Expert 256MB
Hi
I have a probleum in vconverting a string value into date in vb.net
e.g
str="31.12.2005"
You can conver using ToString("MM/dd/yyyy")
Expand|Select|Wrap|Line Numbers
  1. str="31.12.2005"
  2. DateTime thisDate = Convert.ToDateTime(str.ToString("MM/dd/yyyy"))
  3.  
Jun 3 '08 #2
veer
198 100+
Hi
thanks for reply
i used your code like this
Expand|Select|Wrap|Line Numbers
  1.  Dim str As String 
  2. Dim thisdate As DateTime
  3. Str = "31.12.2005"
  4. thisdate=Convert.ToDateTime(str.ToString("MM/dd/yyyy"))
  5.  
it shows the following error

Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.
Jun 3 '08 #3
CyberSoftHari
487 Expert 256MB
Okay Use like
Expand|Select|Wrap|Line Numbers
  1. thisdate = DateTime.Parse(str.ToString("MM/dd/yyyy"))
Jun 3 '08 #4
Curtis Rutland
3,256 Expert 2GB
This is what you need to do. You need to parse your string, and then create a DateTime object from the pieces.
Expand|Select|Wrap|Line Numbers
  1. string s = "31.12.2008";
  2. string[] delims = { "." };
  3. string[] tokens = s.Split(delims,StringSplitOptions.None);
  4. DateTime d = DateTime.Parse(String.Format("{0}/{1}/{2}", tokens[1], tokens[0], tokens[2]));
  5.  
Hi
I have a probleum in vconverting a string value into date in vb.net
e.g
str="31.12.2005"
i want to convert it into date format like "mm/dd/yyyy"
i use CDATE function for it but every time error comes " String can't be converted into date"

but when i use any other value like 12.31.2005 it works fine

how can i change the first str value into 2nd value because from where i am accessing the always have the value "31.12.2006"


please give me some idea
Jun 3 '08 #5
Plater
7,872 Expert 4TB
There is an even easier way.

Expand|Select|Wrap|Line Numbers
  1. string s="31.12.2005";
  2. DateTime dt=DateTime.ParseExact(s, "dd.MM.yyyy", System.Globalization.CultureInfo.CurrentCulture);
  3. MessageBox.Show(dt.ToString("MM/dd/yyyy")); 
  4.  
Jun 3 '08 #6
veer
198 100+
Hi
i made a program in which i want to convert a string into date and make an exe
but when i run this exe on my computer it works fine & display the date
but when i use this exe on any other computer
i display the error
"the string is cannot be converted into date"
i use this line in my coding

Expand|Select|Wrap|Line Numbers
  1.  Dim tempstr As String 
  2. Dim eddate As Date
  3. tempstr = "11/26/2006"
  4. eddate = CDate(tempstr)
  5. MsgBox(eddate)
  6.  
please tell me what is wrong

With reGArds
varinder
Jun 4 '08 #7
shweta123
692 Expert 512MB
Hi,

I think you are getting "String to Date conversion error" because of different datetime formats on each machine. When you use cDate function ,if month you specify in date is not valid you may get the error. So , you have to write the code in your application to get the string with valid date in required date format.
Jun 4 '08 #8
veer
198 100+
Thanks shweta123
but will you give some code example

reGArds
varinder


Hi,

I think you are getting "String to Date conversion error" because of different datetime formats on each machine. When you use cDate function ,if month you specify in date is not valid you may get the error. So , you have to write the code in your application to get the string with valid date in required date format.
Jun 4 '08 #9
Curtis Rutland
3,256 Expert 2GB
You can see some code examples of this on your own thread from yesterday. Some of them are in C# but you should be able to figure out the VB from that.

Plater's example is the easiest way, and if his doesn't work, mine will if you substitute "/" for "."
Jun 4 '08 #10
Plater
7,872 Expert 4TB
I have merged these threads.
Please do not double post your questions, its against the posting guidelines.

MODERATOR


Many solutions have already been outlined for you, have you even tried them?
Jun 4 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Fons Dijkstra | last post by:
Hello, I'm using the mx.ODBC.Windows package in order to read/write a MSAccess database. Everything works fine apart from the DATE format handling. I'm using the default "datetimeformat" (i.e....
6
by: Jerome | last post by:
Hi, I know this is an old problem and I've already tried to look the solution up on the web but I didn't find what I need. So, there's the following situation: I've got an ASP page with a...
1
by: php newbie | last post by:
Hello, I am trying to insert some date values into a table. I am using ODBC prepared statements to (potentially) improve performance. The statement syntax I am using is this: INSERT INTO...
20
by: rhino | last post by:
This worked before October now it does not work. I think I know where the trouble is but I don't know how to fix it. function findVerse() { var d = new Date(); var str; if( (...
11
by: Geoff Jones | last post by:
Hi I have a table that has a column with Date types. I am trying to view certain rows in the table using a DataView. Using the filter, I can view the rows with, for example, the date equal...
3
by: Kevin Hodgson | last post by:
I'm trying to import from a text file into a Sql DB, but I'm getting hung up on a date field. My text file has the date in the dd/mm/yyyy format. I Cast that field to CDate in VB.NET, which sets...
3
by: seegoon | last post by:
Hi to all. I have a small problem I hope someone can help me with. I am running a sql query to a csv file. The query searches for the total of a column between 2 dates. This is a copy of one of...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
17
by: Terry Olsen | last post by:
Given that variable dt = "3/31/2007", why does it produce the following exception on some machines? It works fine on my PC, but others have sent me this exception information because it threw up on...
14
by: Simon Gare | last post by:
Hi, have a search.asp page with results.asp page drawing data from an SQL db, problem is the user has to type the whole field value into the search box to retrieve the value on results.asp, what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.