Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:05:15+00:00 2026-05-30T13:05:15+00:00

Possible Duplicate: Reading Datetime value From Excel sheet I’m trying to read in a

  • 0

Possible Duplicate:
Reading Datetime value From Excel sheet

I’m trying to read in a DateTime value from an Excel spreadsheet using Interop in C#. I have all times in the ‘C’ column of my sheet. My code is as follows:

    public void addTime(Microsoft.Office.Interop.Excel.Workbook workbook)
    {

        Excel.Worksheet ws = (Excel.Worksheet)workbook.Worksheets.get_Item("Time Series");
        Excel.Range range = ws.UsedRange;
        int num = 0;
        for (int row = 1; row <= range.Rows.Count; row++ )
        {
            String dtString = ((Excel.Range)ws.Cells[row, "C"]).Value2.ToString();
            DateTime dt = Convert.ToDateTime(dtString);

            this.addEdgeInstance(dt);
        }
    }

Yet this doesn’t read in the time. Reading of the string works, but the Convert function does not work. Do I have read in values a different way? Can the values vary? (IE: can I enter ’11/11′ or ’11/11/2011′ as Excel recognizes both of these entries as valie DateTime values when working in Excel?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:05:17+00:00Added an answer on May 30, 2026 at 1:05 pm

    There yo go bud. I modified the code for you a little to reflect the usage of the helper conversion function. The conversion happens in this line: DateTime dt = DateTime.Parse(ConvertToDateTime(dtString)); Feel free to modify the helper function as needed to return a DateTime variable instead of a string, but this is essentially the same thing. Hope this helps!

    public void addTime(Microsoft.Office.Interop.Excel.Workbook workbook)  
    {  
    Excel.Worksheet ws = (Excel.Worksheet)workbook.Worksheets.get_Item("Time Series");  
    Excel.Range range = ws.UsedRange;  
    int num = 0;  
    for (int row = 1; row <= range.Rows.Count; row++ )  
    {  
        String dtString = ((Excel.Range)ws.Cells[row, "C"]).Value2.ToString();  
        DateTime dt = DateTime.Parse(ConvertToDateTime(dtString));  
    
        this.addEdgeInstance(dt);  
    }  
    }  
    
    
    public static string ConvertToDateTime(string strExcelDate)
    {
        double excelDate;
        try
        {
            excelDate = Convert.ToDouble(strExcelDate);
        }
        catch
        {
            return strExcelDate;
        }
        if (excelDate < 1)
        {
            throw new ArgumentException("Excel dates cannot be smaller than 0.");
        }
        DateTime dateOfReference = new DateTime(1900, 1, 1);
        if (excelDate > 60d)
        {
            excelDate = excelDate - 2;
        }
        else
        {
            excelDate = excelDate - 1;
        }
        return dateOfReference.AddDays(excelDate).ToShortDateString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Reading dll.config (not app.config!) from a plugin module. I have two different
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: Parsing Dates and Times from Strings using python I'm reading a string
Possible Duplicate: How to read XML response from a URL in java? I'm trying
Possible Duplicate: reading a specific file from sdcard in android I'm trying to make
Possible Duplicate: Reading the last n lines from a huge text file I have
Possible Duplicate: Reading binary file from URLConnection Now I have a URL: ( http://elsa.berkeley.edu/~saez/TabFig2005prel.xls
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Read/write .txt file with special characters Im reading a file but I
Possible Duplicate: how remove wordwrap from textarea I have a textarea which I fill

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.