473,407 Members | 2,312 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,407 software developers and data experts.

converting string to datetime

I need to convert a string as: "DEC 18 2007 2:49:40:783PM"
to a date. This value is sent by SQL server as :select convert(varchar(30),
creationDate, 109) from ... ... .

I tried :
Dim dt As DateTime
Dim myCultureInfo As New CultureInfo("en-US")
If DateTime.TryParseExact("Dec 18 2007 2:49:40:783PM", "mon dd yyyy
hh:mi:ss:mmmAM", myCultureInfo, Globalization.DateTimeStyles.None, dt) Then
.... ...
.... .... ..

always return false (string not a valid date)

how can I do it ?
Dec 18 '07 #1
5 2045
zino wrote:
I need to convert a string as: "DEC 18 2007 2:49:40:783PM"
to a date. This value is sent by SQL server as :select
convert(varchar(30), creationDate, 109) from ... ... .
Just one question: why don't you get SQL Server to return the date in
ISO8601 format instead? This is the only globally identifiable non-ambiguous
format in which dates can be represented as strings.

You can get SQL Server to return it in this format by changing the 109 in
your convert statement to 21 instead. (I <3 magic numbers....)

Then you can just use DateTime.Parse or CDate to convert the string to a
date.

HTH?

--

(O)enone
Dec 18 '07 #2
On 2007-12-18, zino <zi**@noemail.noemailwrote:
I need to convert a string as: "DEC 18 2007 2:49:40:783PM"
to a date. This value is sent by SQL server as :select convert(varchar(30),
creationDate, 109) from ... ... .

I tried :
Dim dt As DateTime
Dim myCultureInfo As New CultureInfo("en-US")
If DateTime.TryParseExact("Dec 18 2007 2:49:40:783PM", "mon dd yyyy
hh:mi:ss:mmmAM", myCultureInfo, Globalization.DateTimeStyles.None, dt) Then
... ...
... .... ..

always return false (string not a valid date)

how can I do it ?

Your format specifier is wrong...

"MMM dd yyyy h:m:s:ffftt"

You need to adjust the m,s in the time, depending on if the minutes are
minutes/seconds are padded. I'm assuming not, because the hour wasn't,
but if they are - then change the m to mm and the s to ss.

--
Tom Shelton
Dec 18 '07 #3
Zino,

Although Tom is in my idea correct answering direct your question, is in my
idea the parameter approach much simpler.

http://www.vb-tips.com/SQLParameters.aspx

Cor

Dec 19 '07 #4
Or even why to return a string at all ? My personal preference would be to
always return a date from SQL Server. Then the date is formatted client side
for display (what if you display those data in a grid and want to sort them
by date ?)

--
Patrice

"(O)enone" <oe****@nowhere.coma écrit dans le message de news:
Uy******************@newsfe5-win.ntli.net...
zino wrote:
>I need to convert a string as: "DEC 18 2007 2:49:40:783PM"
to a date. This value is sent by SQL server as :select
convert(varchar(30), creationDate, 109) from ... ... .

Just one question: why don't you get SQL Server to return the date in
ISO8601 format instead? This is the only globally identifiable
non-ambiguous format in which dates can be represented as strings.

You can get SQL Server to return it in this format by changing the 109 in
your convert statement to 21 instead. (I <3 magic numbers....)

Then you can just use DateTime.Parse or CDate to convert the string to a
date.

HTH?

--

(O)enone

Dec 19 '07 #5
I got it to work with the : convert(varchar(30),creationDate,21) instead of
"109".

thanks for the help
Dec 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify...
10
by: Kim Hellan | last post by:
I have a simple string in the format "DD-MM-YY hh:mm:ss", that I need to convert to a DateTime value. I know this is a standard problem, but please don't just link to all the MSDN pages regarding...
8
by: Mika M | last post by:
Is there better way to convert integer type date into DateTime type date as doing like code below? Dim intDate As Integer = 20051019 Dim dte As DateTime = New DateTime( _...
3
by: NateM | last post by:
How do I convert any given date into a milliseconds value that represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT? Is there an easy way to do this like...
3
by: Rich Robinson | last post by:
Hi, I have a web service method which takes a DateTime type as a parameter. The service is UK based, and the dates are passed in to the service in the UK format dd/MM/yyyy. On a recent...
6
by: B.N.Prabhu | last post by:
Hi, I am having a DataTable which one column is in string format now i want to change that one to DataTime. I don't know whether its in datetime format or not i have to check if it is in...
9
by: Alok yadav | last post by:
i am using a webservice in which a method is serach. i use this method which accept a argument of date type in dd/MM/yyyy formate. i have a textbox which accept the date from the user, when i...
2
by: Brian Parker | last post by:
I am beginning to work with VB2005.NET and I'm getting some problems with string formatting converting an application from VB6. VB6 code:- sTradeDate = Format(pArray(4,i Record), "mmddyy") ...
2
by: joyjignesh | last post by:
hi i have make a report with mshflexgrid. the report between two date. when i have written query .open"select * from tablename where t_date>=convert(datetime,' " & text1.text &"') and ...
1
by: SnehaAgrawal | last post by:
Hi I get the error "Syntax error converting datetime from character string "for the following sql statement in Stored proc. SET @dynamicSQL =N'SELECT CUSTID,SHCodeLink FROM AccountMaster where...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.