# Issues Importing Dates and Times from Excel

**URL:** <https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024>\
**Category:** Questions\
**Tags:** data-science, import-and-export, units\
**Created:** [May 2, 2023, 1:29pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024 "2023-05-02T13:29:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerleventhal](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@rogerleventhal](https://community.wolfram.com/u/rogerleventhal)\
**Post date:** [May 2, 2023, 1:29pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/1 "2023-05-02T13:29:57Z")

</div>

When I import this simple file from Excel, the dates and times are coming in incorrectly (dates are way off). In the Excel file (attached) the dates and times are in different columns (not uncommon) so I would like to import and combine in Mathematica as a single date object for plotting. Since Excel is so commonly used, I would like to see better tools for Importing data for use in Mathematica. I left a few blanks and missing data columns for some help in detecting bad data (also must be common).

Thanks for any help, I know this is probably a beginner question. Roger

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/c0e1a52d-e42d-4f96-89c7-1e7aa4767c5f)

[BP\_readings\_mathematica\_test.xlsx](https://community.wolfram.com/uploads/short-url/bPUHrR0KJ2dyDUPqgIxeUY162l9.xlsx) (17 KB)

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [May 2, 2023, 3:41pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/2 "2023-05-02T15:41:56Z")

</div>

It looks to me like those times (second column) are actually dates. Maybe in Excel choosing to format something as a time also causes the internal representation to become a date. I don’t know what magic Excel is doing under the covers. I would either figure out how to force those dates into actual times, if that’s possible, or just avoid importing the xlsx file directly. It might be safer to export from Excel into csv, and then import the csv.

But, you could also just fix the data once you have it in Mathematica.

```
rawBP = Import[pathToFile, {"Data", 1}, HeaderLines -> 1];
BProws = DeleteCases[rawBP, {"" ...}];

```

We now have non-empty rows. Let’s define a function to combine the dates and “times”:

```
NormalizeDateTime[{date_, time_, rest___}] := 
  {DateObject[Join[DateValue[date, {"Year", "Month", "Day"}], DateValue[time, {"Hour", "Minute", "Second"}]]], rest}

```

And apply it to the data:

```
NormalizeDateTime /@ BProws

```

---

<div class="post-metadata">

**Author:** ![rogerleventhal](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@rogerleventhal](https://community.wolfram.com/u/rogerleventhal)\
**Post date:** [May 2, 2023, 5:11pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/3 "2023-05-02T17:11:32Z")

</div>

Thanks for the suggestions, It worked . Is NormalizeDateTime a built in function? (I searched but it didnt come up) or a user defined function? (I was told to use lower case for user defined functions which is why I ask).

How did it know to join the first two columns with the date join command (is that default?). I was looking for a Part command and it didn’t seem to be needed. And what does “_rest_” do…

And is it easy enough to change GMT to EST with a command? How would one do that?

and yes, Excel can be difficult or a mess to deal with . Unfortunately, it is commonly used for data collection so would be nice if Mathematica could anticipate the idiosyncrasies of Excel just because its so common. I will try saving as a cvs file and see if that works as well.,

thanks so much for your help, Roger

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [May 2, 2023, 5:36pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/4 "2023-05-02T17:36:51Z")

</div>

> Is NormalizeDateTime a built in function?

No. I showed how I defined it.

> I was told to use lower case for user defined functions

Yeah, this is common advice, but it’s silly. The system will alert you if you’ve tried to override a built-in function, so there’s no real danger, and I like maintaining the uniform capitalization.

> How did it know to join the first two columns with the date join command?

Because I used pattern matching. It’s too much to go into as part of this answer. It sounds like you should run through some of the tutorials for how to program in Mathematica.

> And is it easy enough to change GMT to EST with a command?

Look into `TimeZoneConvert`.

---

<div class="post-metadata">

**Author:** ![rogerleventhal](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@rogerleventhal](https://community.wolfram.com/u/rogerleventhal)\
**Post date:** [May 4, 2023, 1:26pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/5 "2023-05-04T13:26:43Z")

</div>

Ok, one last beginner question (I wish there was a basic questions forum). I applied the changes above (thank you) and the data looks correct (I replaced the missing or bad data with “Missing[]” as online guidance suggests). But when I try and plot the data (using DateListPlot) with the date object on the x-axis and one of the data columns (say column 3 (sysBP) as y-axis values it doesn’t work. I tried to transpose the data to create a {date, value} list and that works for all numerical data - but doesn’t seem to work when one column is a date object. This seems so basic, import data with a date stamp and plot against another column of readings. I have read the online guidance but its pretty thin for date object import and plotting. I appreciate any advice on this basic ask.

[Open in Wolfram Cloud ↗](https://www.wolframcloud.com/obj/f082b239-5961-456c-b349-5cda5a59cf9c)

---

<div class="post-metadata">

**Author:** ![eric3](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/eric3/32/12760_2.png) [@eric3](https://community.wolfram.com/u/eric3)\
**Post date:** [May 4, 2023, 3:00pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/6 "2023-05-04T15:00:22Z")

</div>

Let’s start from this point:

```
BPr1= NormalizeDateTime/@BProws

```

Let’s get your plot first, and then later I’ll explain some of the issues with your code.

```
DateListPlot[BPr1[[All, {1, 3}]]]

```

[BPDateListPlot.bmp](https://community.wolfram.com/uploads/short-url/nQgEi3utOY7r81JXbxIfcinX7hA.bmp) (966 KB)

`DateListPlot` can work with a couple different forms, but we have date-value pairs in our data, so we just need extract them and pass them to `DateListPlot`. This is the bit that extracts the pairs:

```
BPr1[[All, {1, 3}]]

```

You’ll want to become familiar with the various forms for `Part` (double brackets are just syntactic sugar for `Part`). `BPr1` is 2-dimensional, so we can use two index specifications separated by a comma. So, `BPr1[[1,3]]` would be the third element in the first row, for example. But `Part` can also take a list for an index specification, and it will fetch according to each index in the list at that level. And then there is the special form of `All`, which is like a list that includes every index at that level. So, `BPr1[[All, {1, 3}]]` is saying “fetch from all rows the 1st and 3rd elements”.

Okay, now let’s critique your code.

- TableForm

- Mutation doesn’t occur automatically

- Pay attention to the forms used in the documentation

---

<div class="post-metadata">

**Author:** ![rogerleventhal](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@rogerleventhal](https://community.wolfram.com/u/rogerleventhal)\
**Post date:** [May 4, 2023, 6:44pm UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/7 "2023-05-04T18:44:12Z")

</div>

Thank you again for your very clear and helpful explanation. You should write a book or a manual for people trying to learn how to use Mathematica.

---

<div class="post-metadata">

**Author:** ![hmichel](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/hmichel/32/31351_2.png) [@hmichel](https://community.wolfram.com/u/hmichel)\
**Post date:** [May 9, 2023, 10:56am UTC](https://community.wolfram.com/t/issues-importing-dates-and-times-from-excel/22024/8 "2023-05-09T10:56:12Z")

</div>

Roger:  
A bit late but hope the following helps.

```auto
SetDirectory[$UserDocumentsDirectory];
data = Transpose[
   Delete[Transpose
     [ReplaceAll[
      Import["BP_readings_mathematica_test.xlsx", 
       {"Data", 2},
        HeaderLines -> 1, 
       "EmptyField" -> Missing[]],
      {" --" -> Missing[], 
       " -" -> Missing[], {Missing[], Missing[], Missing[], Missing[],
          Missing[], Missing[], Missing[]} -> Nothing}]] ,
    3]
   ];
bprange[s_, d_] := Which[
   And[LessEqual[s, 120], LessEqual[d, 80]], 
   RGBColor[0.65, 0.81, 0.22],
   And[Between[s, {121, 129}], LessEqual[d, 80]], RGBColor[1, 0.93, 0],
   Or[Between[s, {130, 139}], Between[d, {81 , 89}]], 
   RGBColor[1, 0.71, 0],
   Or[Between[s, {140, 179}], Between[d, {90 , 119}]], 
   RGBColor[0.73, 0.23, 0.01],
   Or[GreaterEqual[s, 180], GreaterEqual[d, 120]], 
   RGBColor[0.6, 0.03, 0.07]
   ];
createBPseries[{date_, time_, systolic_, diastolic_, pulse_, 
    comment_}] := {
   Tooltip[
    Style[
     {
      DateObject[
       Join[DateValue[date, {"Year", "Month", "Day"}],
        DateValue[time, {"Hour", "Minute", "Second"}]
        ]
       ],
      Interval[{systolic, diastolic}]
      }, bprange[systolic, diastolic]],
    comment]};
{women1839, men1839, women4059, men4059, women60, 
   men60} = {{110, 68}, {119, 70}, {122, 74}, {124, 77}, {139, 
    68}, {133, 69}};
fixeddata = createBPseries /@ data;
DateListPlot[Flatten[fixeddata], 
 Joined -> False,
  PlotRange -> 
  MinMax[Flatten[Transpose[{data[[All, 3]], data[[All, 4]]}]]] + {-5, 
    5}, 
 AspectRatio -> 1/GoldenRatio,
 IntervalMarkers -> "Bars", IntervalMarkersStyle -> Thick,
 GridLines -> {None, men4059}
 ]

```

Changing ` {"Data", 2},` to ` {"Data", 1},` gets the first sheet instead of he second sheet of your spreadsheet.

 ![bp.PNG](https://global.discourse-cdn.com/wolfram/original/3X/f/6/f677680b1e39778ecfef00f5bf771bdf35f9c229.png)
