Computing.Net > Forums > Programming > Batch File-12 hours time format

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File-12 hours time format

Reply to Message Icon

Name: Spiderman
Date: July 29, 2008 at 03:34:46 Pacific
OS: XP
CPU/Ram: 1GB
Product: Dell
Comment:

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!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 29, 2008 at 04:13:06 Pacific
Reply:

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


0

Response Number 2
Name: Spiderman
Date: July 29, 2008 at 04:18:47 Pacific
Reply:

I tried your suggestion and it returns this 14:16:43.94 - military format.

Keep trying!


0

Response Number 3
Name: devil_himself
Date: July 29, 2008 at 04:28:40 Pacific
Reply:

time /t


0

Response Number 4
Name: Mechanix2Go
Date: July 29, 2008 at 05:02:37 Pacific
Reply:

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


0

Response Number 5
Name: Spiderman
Date: July 29, 2008 at 05:20:42 Pacific
Reply:

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!


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: July 29, 2008 at 05:30:56 Pacific
Reply:

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


0

Response Number 7
Name: devil_himself
Date: July 29, 2008 at 05:36:05 Pacific
Reply:

@echo off
echo/|set /p =%date% >time.txt
time /t >>time.txt


0

Response Number 8
Name: Spiderman
Date: July 29, 2008 at 05:36:35 Pacific
Reply:

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!


0

Response Number 9
Name: Spiderman
Date: July 29, 2008 at 05:39:38 Pacific
Reply:

This works great!Thank you all so much!

Keep trying!


0

Response Number 10
Name: Mechanix2Go
Date: July 29, 2008 at 05:59:41 Pacific
Reply:

Call me crazy. I woulda sworn 03:31 was in the morning.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 11
Name: klint
Date: July 29, 2008 at 07:35:05 Pacific
Reply:

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%?


0

Response Number 12
Name: Razor2.3
Date: July 29, 2008 at 18:20:50 Pacific
Reply:

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.


0

Response Number 13
Name: klint
Date: July 30, 2008 at 03:42:09 Pacific
Reply:

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.


0

Response Number 14
Name: Razor2.3
Date: July 30, 2008 at 04:15:51 Pacific
Reply:

klint: I am free to set my time format to mm:hh:ss
Yeah, but what culture actually does so?


0

Response Number 15
Name: Mechanix2Go
Date: July 30, 2008 at 13:29:55 Pacific
Reply:

That would make about as much sense as MMDDYY.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 16
Name: Razor2.3
Date: July 30, 2008 at 17:37:53 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch File-12 hours time format

Batch File runs multiple times www.computing.net/answers/programming/batch-file-runs-multiple-times/16932.html

Batch File - date and time on files www.computing.net/answers/programming/batch-file-date-and-time-on-files/9137.html

Batch file - export current time www.computing.net/answers/programming/batch-file-export-current-time/16555.html