DateTime Exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aamersaeed2368
    New Member
    • Dec 2011
    • 13

    DateTime Exception

    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."

    Code:
     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:
    Code:
    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,
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    for this:

    Code:
    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?

    Comment

    • aamersaeed2368
      New Member
      • Dec 2011
      • 13

      #3
      Dear Adriancs,

      Please see the below mentioned code actually it is a function which will recursively get the list of files from FTP server.

      Code:
       
      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
      Code:
      List<DirectoryItem> listing = GetDirectoryInformation(ftptxtbox.Text, "anonymous", "anonymous");

      Comment

      • adriancs
        New Member
        • Apr 2011
        • 122

        #4
        you can create your own string to DateTime converter. Create a function like this:

        Code:
        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
        Code:
        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
        Code:
        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,
        Code:
        DateTime dateTime = GetDateTime(date);

        Comment

        • adriancs
          New Member
          • Apr 2011
          • 122

          #5
          ...repeated post. can be deleted.

          Comment

          Working...