Best timestamp format for CSV/Excel?

前端 未结 11 2120
盖世英雄少女心
盖世英雄少女心 2020-12-07 16:57

I\'m writing a CSV file. I need to write timestamps that are accurate at least to the second, and preferably to the millisecond. What\'s the best format for timestamps in

相关标签:
11条回答
  • 2020-12-07 17:39

    Try MM/dd/yyyy hh:mm:ss a format.

    Java code to create XML file.

    xmlResponse.append("mydate>").append(this.formatDate(resultSet.getTimestamp("date"), "MM/dd/yyyy hh:mm:ss a")).append("");

    public String formatDate(Date date, String format)
    {
        String dateString = "";
        if(null != date)
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            dateString = dateFormat.format(date);
        }
        return dateString;
    }
    
    0 讨论(0)
  • 2020-12-07 17:42

    "yyyy-mm-dd hh:mm:ss.000" format does not work in all locales. For some (at least Danish) "yyyy-mm-dd hh:mm:ss,000" will work better.

    as replied by user662894.

    I want to add: Don't try to get the microseconds from, say, SQL Server's datetime2 datatype: Excel can't handle more than 3 fractional seconds (i.e. milliseconds).

    So "yyyy-mm-dd hh:mm:ss.000000" won't work, and when Excel is fed this kind of string (from the CSV file), it will perform rounding rather than truncation.

    This may be fine except when microsecond precision matters, in which case you are better off by NOT triggering an automatic datatype recognition but just keep the string as string...

    0 讨论(0)
  • 2020-12-07 17:43

    I believe if you used the double data type, the re-calculation in Excel would work just fine.

    0 讨论(0)
  • 2020-12-07 17:43

    So, weirdly excel imports a csv date in different ways. And, displays them differently depending on the format used in the csv file. Unfortunately the ISO 8061 format comes in as a string. Which prevents you from possibly reformatting the date yourself.

    All the ones the do come in as a date... contain the entire information... but they format differently... if you don't like it you can choose a new format for the column in excel and it will work. (Note: you can tell it came in as a valid date/time as it will right justify... if it comes in as a string it will left justify)

    Here are formats I tested:

    "yyyy-MM-dd" shows up as a date of course when opened in excel. (also "MM/dd/yyyy" works)

    "yyyy-MM-dd HH:mm:ss" default display format is "MM/dd/yyyy HH:mm" (date and time w/out seconds)

    "yyyy-MM-dd HH:mm:ss.fff" default display format is "HH:mm:ss" (time only w/ seconds)

    0 讨论(0)
  • 2020-12-07 17:47

    Given a csv file with a datetime column in this format: yyyy-mm-dd hh:mm:ss

    Excel shows it in this format: dd/mm/yyyy hh:mm

    e.g. 2020-05-22 16:40:55 shows as 22/05/2020 16:40

    This is evidently determined by the Short date and Short time format selected in Windows; for example, if I change the Short date format in Windows to yyyy-mm-dd, Excel shows 2020-05-22 16:40.

    Annoyingly, I can't find any way to make Excel show the seconds automatically (I have to manually format the column in Excel). But if the csv file includes a time column in hh:mm:ss format (e.g. 16:40:55), that's what shows in Excel, including the seconds.

    0 讨论(0)
  • 2020-12-07 17:52

    Go to the language settings in the Control Panel, then Format Options, select a locale and see the actual date format for the chosen locale used by Windows by default. Yes, that timestamp format is locale-sensitive. Excel uses those formats when parsing CSV.

    Even further, if the locale uses characters beyond ASCII, you'll have to emit CSV in the corresponding pre-Unicode Windows "ANSI" codepage, e.g. CP1251. Excel won't accept UTF-8.

    0 讨论(0)
提交回复
热议问题