Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello,
I need a batch file that returns the time in a 12 hours format, like hh:mm AM or hh:mm PM.
I used this code, which returns only hours and minutes without AM/PM and if the hours is less than 10, it doesn't return a "0" in front of it.
echo %time:~0,2%:%time:~3,2% >time.txt
Time settings in Control Panel are in 12 hours format, but still when I run the batch, it returns the time in military format.
Does anyone know how can I change the batch that I currently use so I get the desired result?
Thank you for your time!Keep trying!

Why anobody wants '12 hour' time is beyond me. But moving right along; it may help to:
echo %time%
and post result.
=====================================
If at first you don't succeed, you're about average.M2

First get the hour, in this case 14.
If it
s greater than 11, set your AM/PM to PM.For each of HH MM SS, if the value is one digit, you will need to pad it to two by prepending a zero.
::=================
DH,same thing
=====================================
If at first you don't succeed, you're about average.M2

I used time/t>time.txt and it works great. I have just one additional question: how do I place date on the same row in the text file? I was using echo %date% %time%>time.txt, but now that I changed time format, I don't know how to add date.
I used this time/t>time.txt
I'm pretty new in using this.
Thank you all for your answers!Keep trying!

Echoing into a file has nothing to do with changing date/time format
If you do this, it goes to screen:
echo %time% %date%
If you do this it goes to file:
echo %time% %date% > somefile
=====================================
If at first you don't succeed, you're about average.M2

Yes,I understand that. What I mean is that I changed the code echo %time% %date%>file.txt with time/t>file.txt, but I also need to add the date to the 12 hours time format, so it returns in a text file something like: Tue 07/29/2008 03:31 PM
Any suggestions?Thanks!Keep trying!

Call me crazy. I woulda sworn 03:31 was in the morning.
=====================================
If at first you don't succeed, you're about average.M2

Now here's a rhetorical question (for the designers/implementors of cmd.exe, not that I would ever seriously think they might be reading this forum.)
Whereas the format of the time/t command depends on your Regional settings in Control Panel, the format of %time% is always the same hh:mm:dd.hh 24-hour format. This makes it easier to interpret the time in a batch file, since it doesn't depend on your region setting.
So why isn't this the same with %date%?

klint: So why isn't this the same with %date%?
Because 1/2/08 can be read as either the second day of January, or as the first day of February. Time of day does not have this problem.

Razor2.3: Because 1/2/08 can be read as either the second day of January, or as the first day of February.
No, I mean why haven't Microsoft decided on a specific, fixed, format for the variable %date% - whether they chose mm-dd-yyyy or dd/mm/yyyy or whatever, it doesn't matter what format, but if it was always the same then it would be easy to use the date in a batch file.
By analogy, %time% always has the same format even though the "time/t" differs according to regional settings. However, this does not translate to dates. In contrast to %time%, the format of %date% varies in line with the "date/t" command.
Razor2.3: Time of day does not have this problem.
It does, potentially. I am free, should I so wish, to go into Control Panel, Regional Settings, and set my time format to mm:hh:ss. Not that anyone in their right mind would do that, but then us Europeans would not have believed that anyone in their right mind would choose a mm-dd-yyyy date format either.

That would make about as much sense as MMDDYY.
=====================================
If at first you don't succeed, you're about average.M2

Speaking on behalf of every American, I'd just like to say it's the rest of the world that's backwards, in both your date representation, and your 'metric.'
God, I hope everyone notices the sarcasm. My only hope is if only the regulars read this post.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |