Solved

Get date in powershell in the format d.M.yyyy hh:mm AM/PM

Posted on 2014-02-23
3
2,185 Views
Last Modified: 2014-02-23
I am trying to get the date in this format

(get-date).AddMinutes(10).ToString("d.M.yyyy hh:mm tt")

Where tt should be AM or PM. But I guess tt is wrong to use here.

All help appreciated.
0
Comment
Question by:itnifl
3 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 370 total points
ID: 39880737
Hi,
you need to pass a culture info - otherwise it could happen that you do not get the AM/PM (like on my computer where I have german settings):
$enus = 'en-US' -as [Globalization.CultureInfo]
(get-date).AddMinutes(10).ToString("d.M.yyyy hh:mm tt", $enus)

Open in new window

HTH
Rainer
0
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39880838
Your Code is correct:

PS G:\Documents\WindowsPowerShell\Scripts> (get-date).addminutes(10).tostring("d.M.yyyy hh:mm tt")
23.2.2014 01:03 PM
PS G:\Documents\WindowsPowerShell\Scripts>

Open in new window

0
 
LVL 2

Author Comment

by:itnifl
ID: 39880845
David Johnson, CD, MVP:
But not on my system, most likely because I am not using a system with US local settings. I tested Rainer Jeschor solution, and it seems to work by setting these settings manually in the script.

But thank you anyway =)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Know what services you can and cannot, should and should not combine on your server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question