Is there a way to format date-time with proper alignment?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
I would like to be able to format timestamps (date + time) so that both align but without leading zeroes for either. I do not see an easy way to do this. I will provide both a screenshot and a minisheet because the minisheets do not always preserve the look of the actual sheet, although this time it is pretty good.
  • Column B has the time stamp formatted as "m/dd/yy h:mm AM/PM". The alignment of the dates is off.
  • In Column C, I made the time field 9 characters regardless of the time, but apparently, spaces are not as wide as numbers in variable-spaced fonts,
  • Column D uses a mono-spaced font. The alignment works, but has a different look.
  • Column E solves the problem by changing to 24-hour time. The alignment works, but has a different look.
  • Column F uses a Rube Goldberg solution that seems to work because the space character appears to be half as wide as the number characters.
Is there a better way?

1717699726141.png


Here's the minisheet.
Medical Logs.xlsx
BCDEF
2BCDEF
3Date & TimeFormat TryFormatted MonoFormatted 24 hr2 spaces = 1
412/12/23 10:30 PM12/12/23 10:30 PM12/12/23 10:30 PM12/12/23 22:3012/12/23 10:30 PM
52/03/24 6:03 AM2/03/24 6:03 AM2/03/24 6:03 AM2/03/24 06:032/03/24 6:03 AM
63/28/24 11:44 AM3/28/24 11:44 AM3/28/24 11:44 AM3/28/24 11:443/28/24 11:44 AM
MrExcel
Cell Formulas
RangeFormula
C4:D6C4=TEXT([@[Date & Time]],"m/dd/yy") & RIGHT(" " & TEXT([@[Date & Time]],"h:mm am/pm"),9)
E4:E6E4=[@[Date & Time]]
F4:F6F4=LET(time, TEXT([@[Date & Time]],"h:mm am/pm"), TEXT([@[Date & Time]],"m/dd/yy") & LEFT(" ",2*(9-LEN(time))) & time)
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I created a Conditional Formatting with this formula: =HOUR(F1)<10 and with this formatting: m/d/yy __ h:mm AM/PM

The standard number formatting I applied was: m/d/yy h:mm AM/PM
 
Upvote 0
I created a Conditional Formatting with this formula: =HOUR(F1)<10 and with this formatting: m/d/yy __ h:mm AM/PM

The standard number formatting I applied was: m/d/yy h:mm AM/PM
I couldn't get this to work. Here's how it looks. It's in Column G:

1717832285806.png


Here's the mnisheet:

Cell Formulas
RangeFormula
C4:D6C4=TEXT([@[Date & Time]],"m/dd/yy") & RIGHT(" " & TEXT([@[Date & Time]],"h:mm am/pm"),9)
E4:E6,G4:H6E4=[@[Date & Time]]
F4:F6F4=LET(time, TEXT([@[Date & Time]],"h:mm am/pm"), TEXT([@[Date & Time]],"m/dd/yy") & LEFT(" ",2*(9-LEN(time))) & time)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
H4:H6Expression=HOUR($H$4)<10textNO


And this is the applied formatting:

1717832408700.png
 
Upvote 0
Try this

Book1 (3).xlsx
BC
1
2BC
3Date & TimeConditional
412/12/2023 10:30 PM12/12/2023 10:30 PM
52/3/2024 6:03 AM2/03/2024 6:03 AM
63/2/2024 11:44 AM3/02/2024 11:44 AM
73/29/2024 7:30 AM3/29/2024 7:30 AM
8
9MOD(HOUR(C4),12)<10m/dd/yyyy" " h:mm AM/PM
Sheet2
Cell Formulas
RangeFormula
C4:C7C4=B4
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C4:C7Expression=MOD(HOUR(C4),12)<10textNO
 
Upvote 0
Solution
Upvote 0
Ok. I've done some testing and I think I have gotten the various solutions accurately represented below.

Cell Formulas
RangeFormula
C4:D10,F4:F10,H4:J10C4=[@Standard]
E4:E10E4=MOD(HOUR([@[Conditonal 1]]),12)<10
G4:G10G4=HOUR([@[Conditional 2]])<10
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F4:F10Expression=HOUR($F4)<10textNO
D4:D10Expression=MOD(HOUR($D4),12)<10textNO


This table provides some explanation.

Date & Time.xlsx
LMNOPQ
12ColumnQualityFormat [Conditional Format]ConditionFontComments
13StandardPoorm/dd/yy h:mm AM/PMn/aAny
1424 hourOkm/dd/yy hh:mmn/aAnyGood if 24-hour format is Ok
15Conditonal 1Goodm/dd/yy h:mm AM/PM m/dd/yy" " h:mm AM/PM=MOD(HOUR($D4),12)<10AnyAdd 2 spaces if 1-digit hour
16Conditional 2Goodm/dd/yy_0h:mm AM/PM m/dd/yy_0_0h:mm AM/PM=HOUR($F4)<10AnyAdd 1 digit-space if 1-digit hour
17*Poorm/dd/yy * h:mm AM/PMn/aAny
18DateGoodm/dd/yyn/aAnyDate in separate column
19TimeGoodh:mm:ssn/aAnyTime in separate column
D&T Align Same Cell
Cell Formulas
RangeFormula
L13L13=TblDT01[[#Headers],[Standard]]
L14L14=TblDT01[[#Headers],[24 hour]]
L15L15=TblDT01[[#Headers],[Conditonal 1]]
L16L16=TblDT01[[#Headers],[Conditional 2]]
L17L17=TblDT01[[#Headers],[*]]
L18L18=TblDT01[[#Headers],[Date]]
L19L19=TblDT01[[#Headers],[Time]]


The conditional solutions are too flakey. If I move a column, they can get screwed up. I'll probably either go with the 24-hour format or putting the time in a separate column.

Thanks for all the help.
 
Upvote 0
After a bit more testing, I decided to split the solutions into 2 separate sheets: one for formatting the timestamp in its own cell and one for formatting it somewhere else. The mini-sheets are below. Each sheet has two tables. One has the various solutions. The other has information and comments on those solutions. The solution tables are followed by a second table showing that the timestamp data is still valid by adding 1 day.

If anyone sees any errors or knows of any different solutions, please let me know.

I create these example workbooks for my own use so I don't have to go through the learning curve again in the future. I post them here in case they might be of use to anyone else.

Here is the Solution Table from the Format in the Same Cell sheet.

Cell Formulas
RangeFormula
C4:D10,F4:F10,H4:J10C4=[@Standard]
E4:E10E4=MOD(HOUR([@[Conditional 1]]),12)<10
G4:G10G4=HOUR([@[Conditional 2]])<10
B13:D13,H13:I13,F13B13=B4+1
J13J13=J4+1/24
Cells with Conditional Formatting
CellConditionCell FormatStop If True
F13Expression=HOUR($F13)<10textNO
D13Expression=MOD(HOUR($D13),12)<10textNO
F4:F10Expression=HOUR($F4)<10textNO
D4:D10Expression=MOD(HOUR($D4),12)<10textNO


And this is its Comment Table.

Date & Time.xlsx
LMNOPQ
12ColumnQualityFormat [Conditional Format]ConditionFontComments
13StandardPoorm/dd/yy h:mm AM/PMn/aAnySource data
1424 hourOkm/dd/yy hh:mmn/aAnyGood if 24-hour format is Ok
15Conditional 1Goodm/dd/yy h:mm AM/PM m/dd/yy" " h:mm AM/PM=MOD(HOUR($D4),12)<10AnyAdd 2 spaces if 1-digit hour
16Conditional 2Goodm/dd/yy_0h:mm AM/PM m/dd/yy_0_0h:mm AM/PM=HOUR($F4)<10AnyAdd 1 digit-space if 1-digit hour
17* FormatPoorm/dd/yy * h:mm AM/PMn/aAny
18DateExcellentm/dd/yyn/aAnyDate in separate column
19TimeExcellenth:mm:ss AM/PMn/aAnyTime in separate column
D&T Align Same Cell
Cell Formulas
RangeFormula
L13L13=TblDT01[[#Headers],[Standard]]
L14L14=TblDT01[[#Headers],[24 hour]]
L15L15=TblDT01[[#Headers],[Conditional 1]]
L16L16=TblDT01[[#Headers],[Conditional 2]]
L17L17=TblDT01[[#Headers],[* Format]]
L18L18=TblDT01[[#Headers],[Date]]
L19L19=TblDT01[[#Headers],[Time]]
N13N13=GetFormat(TblDT01[Standard])
N14N14=GetFormat(TblDT01[24 hour])
N15N15=GetFormat(TblDT01[Conditional 1])&" m/dd/yy"" "" h:mm AM/PM"
N16N16=GetFormat(TblDT01[Conditional 2])&" m/dd/yy_0_0h:mm AM/PM"
N17N17=GetFormat(TblDT01[* Format])
N18N18=GetFormat(TblDT01[Date])
N19N19=GetFormat(TblDT01[Time])


Here is the Solution Table from the Format in a Different Cell sheet.

Cell Formulas
RangeFormula
C4:C9C4=TEXT([@[Date & Time]],"m/dd/yy") & RIGHT(" " & TEXT([@[Date & Time]],"h:mm am/pm"),9)
D4:D9D4=[@[Date & Time]]
E4:E9E4=LET(time, TEXT([@[Date & Time]],"h:mm am/pm"), TEXT([@[Date & Time]],"m/dd/yy") & LEFT(" ",2*(9-LEN(time))) & time)
F4:F9F4=TEXT([@[Date & Time]],"m/dd/yy") & RIGHT(" " & TEXT([@[Date & Time]],"h:mm am/pm"),10)
B12:F12B12=B4+1


And this is its Comments Table.

Date & Time.xlsx
HIJKL
3ColumnQualityFormatFontComments
4Date & TimePoorm/dd/yy h:mm AM/PMProportionalSource data
5Mono TextOkm/dd/yy h:mm a/pMonospacedMuch wider
624 hrOkm/dd/yy hh:mmProportionalperfect alignment
72 spaces = 1GoodGeneralProportionalComplex formula
8TextPoorm/dd/yy h:mm a/pProportionalI couldn't get this to work
D&T Align Other Cell
Cell Formulas
RangeFormula
H4H4=TblDT02[[#Headers],[Date & Time]]
H5H5=TblDT02[[#Headers],[Mono Text]]
H6H6=TblDT02[[#Headers],[24 hr]]
H7H7=TblDT02[[#Headers],[2 spaces = 1]]
H8H8=TblDT02[[#Headers],[Text]]
J4J4=GetFormat(TblDT02[Date & Time])
J5J5=GetFormat(TblDT02[Mono Text])
J6J6=GetFormat(TblDT02[24 hr])
J7J7=GetFormat(TblDT02[2 spaces = 1])
J8J8=GetFormat(TblDT02[Text])
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top