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

from string to datetime

Hello dear Cor or anyone around!:)

This didn't help me(convert) : I have 3 strings in an array :
str[0]='11/02/04' ,str[1]='11:23:00" and str[2]=AM. How do I convert
str[0]+str[1]+str[2] to a proper datetime variable. Thanks a lot!
Nov 16 '05 #1
6 2389
juli,

Please don't call my name in this newsgroup.
However I thought that this was the answer on your question.
\\\
string[] str = {"11/02/04","11:23:00","AM"};
string mystring = String.Join(" ",str);
///
I hope this helps?

Cor
Nov 16 '05 #2
juli <ji****@gmail.com> wrote:
Hello dear Cor or anyone around!:)

This didn't help me(convert) : I have 3 strings in an array :
str[0]='11/02/04' ,str[1]='11:23:00" and str[2]=AM. How do I convert
str[0]+str[1]+str[2] to a proper datetime variable. Thanks a lot!


Try this:

using System;
using System.Globalization;

class Test
{
static void Main()
{
string[] bits = {"11/02/04", "11:23:00", "AM"};

string combined = bits[0]+" "+bits[1]+" "+bits[2];

Console.WriteLine (combined);
DateTime dt = DateTime.ParseExact (combined,
"dd/MM/yy hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine (dt);
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
John:

I'm liking your post the best -but I just saw the other two responses and
figured I'd throw in what I aleady had:

I'd make a function out of it so when it comes up again - I have it there:
private DateTime GetDateFromStringArray(System.String[] dateParts)

{

//Should probably validate the inputs here to make sure

//you only have good data to begin with otherwise - throw exception
System.String ReturnValue = null;

for(System.Int16 x = 0; x < dateParts.Length; x++)

{

ReturnValue += dateParts[x] + " ";

}

return DateTime.Parse(ReturnValue);

}

Here's a simple call - I'd also throw in some verification logic to make
sure each piece is a valid time component - otherwise we may get the whole
way to then end and then fail.

I do in fact like your approach - just figured I'd chime in to make things
interesting.
--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
juli <ji****@gmail.com> wrote:
Hello dear Cor or anyone around!:)

This didn't help me(convert) : I have 3 strings in an array :
str[0]='11/02/04' ,str[1]='11:23:00" and str[2]=AM. How do I convert
str[0]+str[1]+str[2] to a proper datetime variable. Thanks a lot!


Try this:

using System;
using System.Globalization;

class Test
{
static void Main()
{
string[] bits = {"11/02/04", "11:23:00", "AM"};

string combined = bits[0]+" "+bits[1]+" "+bits[2];

Console.WriteLine (combined);
DateTime dt = DateTime.ParseExact (combined,
"dd/MM/yy hh:mm:ss tt",
CultureInfo.InvariantCulture);
Console.WriteLine (dt);
}
}
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Cor - with all due respect - this approach is a little challenged.

The main thing is that the concatenated value your method creates is still a
string - it will have to be parsed to be a DateTime like she needs. More
importantly though - there's no validation on those values the way you're
doing it - so if the three values were Juli, Bill, Cor instead of the
dates - your method would work fine. You need a Parse to ensure this
doesn't happen - and in real apps - I'd parse each piece up front to make
sure that I knew my final conversion would be solid - if even one paramater
was junk - I'm not going to be able to do much of anything.

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
"Cor Ligthert" <no************@planet.nl> wrote in message
news:#j**************@TK2MSFTNGP12.phx.gbl...
juli,

Please don't call my name in this newsgroup.
However I thought that this was the answer on your question.
\\\
string[] str = {"11/02/04","11:23:00","AM"};
string mystring = String.Join(" ",str);
///
I hope this helps?

Cor

Nov 16 '05 #5
Bill,

This is a post which is in a kind of continuing of some other post.
However there was datetime variable in the post and I readed that as a
string this time, probably because I had already answered with a sample in
two earlier post how to make of a datetime variable from a string.

However I understand why you wrote this, in this earlier posts was (str was
dt1)

DateTime dta = Convert.ToDateTime(str,null);

So this becomes than complete
\\
string[] str = {"11/02/04","11:23:00","AM"};
string mystring = String.Join(" ",str);
DateTime dta = Convert.ToDateTime(str,null);
///

In my opinion it is this a lot nicer code than any concationation or
stringbuilder loop.
(While you know that I am one of the last who avoid loops)

However just my thought,

Cor
Nov 16 '05 #6
Bill,

I saw there was an typo in my message.
\\
string[] str = {"11/02/04","11:23:00","AM"};
string mystring = String.Join(" ",str);
DateTime dta = Convert.ToDateTime(mystring,null);
///

This gives me the change to tell you something about dates what happened in
the VB language group and you will be probably very happy with the end
conclussion.

Me poor had a discussion with 3 real dotNet MVP's. And not a short one and
very deep. My point with this is always culture independent date
conversions. However those MVP's told that I did not understand it.

And than at a certain moment there came a message from somebody from
Australia, who told, Cor I agree with you. That is the reason that we have
changed all our US software for European.

:-)))

Cor

Nov 16 '05 #7

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

Similar topics

6
by: moonriver | last post by:
I write a program accessing files in network drive o:. It is doable as a standalone application. However, if it is running under windows service, the following exception will appear: 13/07/2004...
16
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...
2
by: Sterling Ledet | last post by:
I am trying to create a web service that takes a string from my web server in the following format: Mon, 6 Oct 2003 18:39:47 UTC and put's in a datetime so it can then be reformatted in C# as...
4
by: Hans Merkl | last post by:
Does anybody know of a library that can handle strings pf various formats and conver them to a DateTime value? The strings are coming from a webform and I can't restrict the input (it's not my...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
4
by: RSH | last post by:
Hi, I have a situation where I have created a little application that makes an Access database from a SQL Database for reporting purposes. it does the job well, but it seems a bit slow. Is...
6
by: Rushwire | last post by:
Does anybody know how to send a meeting request using an ics/vcs (VCalendar) attachment from an asp.net page. I don't want my users to have to double click on the attachment but rather that it is...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: John Dann | last post by:
Is there a concise/efficient way to retrieve blocks of rows from a datatable with VB2005? I've got a datatable (let's call it AllData), constructed programmatically, that contains a lot of...
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
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
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...
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,...

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.