Dear All,
The below mentioned line throw an exception that "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." - DateTime dateTime = DateTime.Parse(date);
-
Actually this error is shown in my home pc where i am using window xp and visual 2010........... .........howeve r in my university lab on window 7 it works fine by passing second parameter as System.Globaliz ation.culturein fo.createspecif icculture("en-US").DateTimeFo rmat.Like: - DateTime dateTime = DateTime.Parse(date, System.Globalization.CultureInfo.CreateSpecificCulture("en-US").DateTimeFormat);
Please anyone help me regarding this exception.:(
Hope you understand my question.
Thanks,
4 1248
for this: - DateTime dateTime = DateTime.Parse(date);
where do you get the value of this variable " date" in the above statement?
Where does the value come from?
Dear Adriancs,
Please see the below mentioned code actually it is a function which will recursively get the list of files from FTP server. -
-
struct DirectoryItem
-
{
-
public Uri BaseUri;
-
-
public string AbsolutePath
-
{
-
get
-
{
-
return string.Format("{0}/{1}", BaseUri, Name);
-
}
-
}
-
public DateTime DateCreated;
-
public bool IsDirectory;
-
public string Name;
-
public List<DirectoryItem> Items;
-
}
-
-
public List<DirectoryItem> GetDirectoryInformation(string address, string username, string password)
-
{
-
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);
-
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
-
request.Credentials = new NetworkCredential(username, password);
-
request.UsePassive = true;
-
request.UseBinary = true;
-
request.KeepAlive = false;
-
-
List<DirectoryItem> returnValue = new List<DirectoryItem>();
-
string[] list = null;
-
-
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
-
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
-
{
-
list = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
-
}
-
-
foreach (string line in list)
-
{
-
// Windows FTP Server Response Format
-
// DateCreated IsDirectory Name
-
string data = line;
-
-
// Parse date
-
string date = data.Substring(0,17);
-
DateTime dateTime = DateTime.Parse(date);
-
-
data = data.Remove(0, 24);
-
-
// Parse <DIR>
-
string dir = data.Substring(0, 5);
-
bool isDirectory = dir.Equals("<dir>", StringComparison.InvariantCultureIgnoreCase);
-
data = data.Remove(0, 5);
-
data = data.Remove(0, 10);
-
-
// Parse name
-
string name = data;
-
-
// Create directory info
-
DirectoryItem item = new DirectoryItem();
-
item.BaseUri = new Uri(address);
-
item.DateCreated = dateTime;
-
item.IsDirectory = isDirectory;
-
item.Name = name;
-
-
-
// Debug.WriteLine(item.AbsolutePath);
-
-
item.Items = item.IsDirectory ? GetDirectoryInformation(item.AbsolutePath, username, password) : null;
-
-
f.listboxFiles.Items.Add(name);
-
-
returnValue.Add(item);
-
}
-
-
return returnValue;
-
}
And call to this function is - List<DirectoryItem> listing = GetDirectoryInformation(ftptxtbox.Text, "anonymous", "anonymous");
you can create your own string to DateTime converter. Create a function like this: - public static DateTime GetDateTime(string date)
-
{
-
// assume that
-
// date = "2012-02-21 21:29:30";
-
int year = Convert.ToInt32(data.Substring(0, 4));
-
int month = Convert.ToInt32(data.Substring(5, 2));
-
int day = Convert.ToInt32(data.Substring(8, 2));
-
int hour = Convert.ToInt32(data.Substring(11, 2));
-
int minute = Convert.ToInt32(data.Substring(14, 2));
-
int second = Convert.ToInt32(data.Substring(17, 2));
-
return new DateTime(year, month, day, hour, minute, second);
-
}
or - public static DateTime GetDateTime(string date)
-
{
-
// assume that
-
// date = "2012-02-21-21-34-30";
-
string[] sa = data.Split('-');
-
int[] ia = new int[sa.Length];
-
for (int i = 0; i < sa.Length; i++)
-
{
-
ia[i] = Convert.ToInt32(sa[i]);
-
}
-
return new DateTime(ia[0], ia[1], ia[2], ia[3], ia[4], ia[5]);
-
}
or - public static DateTime GetDateTime(string date)
-
{
-
// assume
-
// date = "21 Feb 2012, 09:42 PM";
-
string[] sa = date.Split(',');
-
string[] separator1 = new string[] { " " };
-
string[] sb = sa[0].Trim().Split(separator1, StringSplitOptions.None);
-
-
int day = Convert.ToInt32(sb[0]);
-
-
int month = 0;
-
-
switch (sb[1].ToUpper())
-
{
-
case "JAN":
-
month = 1;
-
break;
-
case "FEB":
-
month = 2;
-
break;
-
-
.....
-
-
default:
-
break;
-
}
-
-
int year = Convert.ToInt32(sb[2]);
-
-
string[] sc = sa[1].Split(':');
-
-
int hour = Convert.ToInt32(sc[0]);
-
-
string[] sd = sc[1].Split(separator1, StringSplitOptions.None);
-
-
int minute = Convert.ToInt32(sd[0]);
-
-
if (sd[1].ToUpper() == "PM")
-
hour += 12;
-
-
return new DateTime(year, month, day, hour, minute, 0);
-
}
then, - DateTime dateTime = GetDateTime(date);
...repeated post. can be deleted.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dan S |
last post by:
My application asks the user to enter in a date - in the mm/dd/yyyy format. Is there any quick and easy way to verify the date the user enters is formatted correctly? Right now I'm calling DateTime.Parse() and catching the FormatException but it seems this is a bit inefficient - catching the exception that is. There is some pretty obvious delay while it traces back up the call stack. Is there a better way? Something that returns a bool...
|
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 form).
I have been using Convert.ToDateTime but it choked on "12:00 noon".
I am looking for a function that can make sense out of anything that looks
like a date/time. Outlook is pretty good at it when you enter an
appointment with the GUI, but...
|
by: adolf garlic |
last post by:
I'm having a problem with dates.
(No not the sort that you have at christmas)
I have a webform with a text box and a calendar control.
You can either
- enter a date manually (we are talking uk style here dd/mm/yyyy) in the
textbox
- click a date on the calendar which puts the date value in the textbox (uk
style)
|
by: Glenn Venzke |
last post by:
I have an aspx page that is set up to copy backed-up DB files from a shared
directory to a local folder. For some reason, it is being denied access to
the network share. I have the web app running under a domain account that I
know for a fact has access. it works fine when I log on to the network and
browse the directory manually. Even when I grant full control to everyone on
the share and the underlying folder itself, I still can't access...
|
by: news.microsoft.com |
last post by:
dim d as datetime
d = DateTime.Parse(#6/10/2004 11:50:40 PM#) produces exception "String was
not recognized as a valid DateTime."
d = DateTime.Parse("6/10/2004 11:50:40 PM") is OK
d = DateTime.Parse(#6/10/2004 11:50:40 AM#) is OK
can somebody expain what is wrong with the first statement
I am using VS 2003 with framework version 1.1 sp1
Thanx George
| |
by: Cor Ligthert |
last post by:
Hello everybody,
Jay and Herfried are telling me every time when I use CDate that using the
datetime.parseexact is always the best way to do String to datetime
conversions. They don't tell why only that I have to listen to them because
they know it better.
They told also that in a business situation it is better to use
datetime.parseexact for changing cultures and not to use the globalization
setting. I did not give them this sample,...
|
by: Tim |
last post by:
Hi,
I am trying to do something simple. Convert a string date to datetime but it
is not working and is giving me a baffling error!
System.Convert.ToDateTime("Jan 30, 2006")
'System.Convert.ToDateTime("Jan 30, 2006")' threw an exception of type
'System.IndexOutOfRangeException'
base {System.SystemException}: {"Index was outside the bounds of the
array."}
|
by: GG |
last post by:
I am trying to add a nullable datetime column to a datatable fails. I am
getting exception
DataSet does not support System.Nullable<>.
None of these works
dtSearchFromData.Columns.Add( new DataColumn( "StartDate",
typeof( DateTime? ) ) );
dtSearchFromData.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable<DateTime>) ) );
Any ideas?
|
by: =?Utf-8?B?UGFvbG8=?= |
last post by:
I have a SQL database table with rows in which the primary key is a DateTime
type (although I don't use the Time part). Have added numerous rows and now
want to delete one so I enter the date (dd/mm/yyyy) for a row I know is in
the database but my app tells me that no row exists for that date.
Am I missing something in relation to this data type?
|
by: csharpula csharp |
last post by:
Hello,
I am trying to add DataTime.Now.ToString() to a file name but I get an
exception while doing it that such format is not supported in path. How
can I solve this and cast this issue?
Thank you!
*** Sent via Developersdex http://www.developersdex.com ***
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |