Date Time format displays differently in ISE and Windows Forms - powershell

When I run Get-Date in ISE, I get Wednesday, 15 April 2020 12:38:03 PM which I want.
However, if I run the same command in Windows Forms, I get 04/15/2020 12:38:03 in a different format.
I run them from the same computer so it must be the same cultural/region.

1. Customizing your date using -Format or -UFormat
You can use the -Format or the -UFormat paramater to enforce a certain layout of your date:
Get-Date -Format "dddd, d MMMM yyyy hh:mm:ss tt"
Get-Date -UFormat "%A, %e %B %Y %r"
Both will display your desired date format, as long as you are using en-US culture information:
Wednesday, 15 April 2020 08:09:24 AM
Learn more about:
.NET format specifiers
UFormat specifiers
2. Customizing your date with different culture information
If you want to display the date in a different language, you can also enforce a certain culture information. Keep in mind that the -Format parameter is just a wrapper for the ToString() method. So you can also use the following line to display your date as desired:
(Get-Date).ToString('dddd, d MMMM yyyy hh:mm:ss tt')
Fortunately, there exist different overloads of that ToString() method. There is also one, that takes culture information as a second parameter. So in conclusion you can pass different culture info to your ToString() method to get results in different languages:
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('en-US')
(Get-Date).ToString('dddd, d MMMM yyyy hh:mm:ss tt', $culture)
will display:
Wednesday, 15 April 2020 08:09:24 AM
and at the same time
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('de-DE')
(Get-Date).ToString('dddd, d MMMM yyyy hh:mm:ss tt', $culture)
will display:
Mittwoch, 15 April 2020 08:09:24
3. Customizing your date with predefined culture specific patterns
In $culture.DateTimeFormat you can also find already prepared culture specific patterns to format your date and you can use them instead of writing them on your own:
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('en-US')
(Get-Date).ToString($culture.DateTimeFormat.ShortDatePattern, $culture)
will display:
4/15/2020
and at the same time
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('de-DE')
(Get-Date).ToString($culture.DateTimeFormat.ShortDatePattern, $culture)
will display:
15.04.2020
Btw: A similar pattern to yours, specified in your question, would be:
$culture = [System.Globalization.CultureInfo]::CreateSpecificCulture('en-US')
(Get-Date).ToString($culture.DateTimeFormat.FullDateTimePattern, $culture)
Wednesday, April 15, 2020 8:09:24 AM

Related

Setting timezone = "Asia/Kolkata" in #jsonformat and storing it in java.sql.timestamp converts 12 pm to 1 pm timesatmp to 00 am in spring hibernate

I am using
#JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss", timezone = "Asia/Kolkata")
private Timestamp startDateTime;
to store timestamp comes in json as a string.
But the problem is it converts time between 12 pm to 1 pm into 00 am.
E.g. 2021-10-25 12:30:00 gets converted to 2021-10-25 00:30:00.
Any help will be appreciated.
Thank you.
The root cause of the problem is that you have used h instead of H. Note that h is used for 12-hour time format (i.e. time with AM/PM marker) while H is used for 24-hour time format. So, the solution to your problem is to change the format to dd-MM-yyyy HH:mm:ss.
In case, you want the AM/PM marker in the time, change the format to dd-MM-yyyy hh:mm:ss a.

Powershell what is the time format

2017-05-05 12:03:02 -0400 (Fri, 05 May 2017)
what time format is the above in powershell??
i need the same format to compare with today time in powershell.
Get-Date -format
Since this is a follow-up question, you should mention this.
To convert a string containing a datetime based on UTC with a timezone offset,
use [datetime]::ParseExact(). You should change the RegEx from previous Q&A to only contain the datetime without the redundant information in parentheses.
[datetime]::ParseExact("2017-05-05 12:03:02 -0400","yyyy-MM-dd HH:mm:ss zzzzz",$Null)
This will convert to local time (and correctly include the DST aka daylight saving time at that date)
Sample output here with timezone +1 and DST active to:
2017-05-05 18:03:02 +02:00
Or more universal with .ToString('U') and my German locale settings to:
Freitag, 5. Mai 2017 16:03:02
To get the difference to [datetime]::Now simply subtract and choose a property from
> [datetime]::Now - [datetime]::ParseExact("2017-05-05 12:03:02 -0400","yyyy-MM-dd HHH:mm:ss zzzzz",$Null)
Days : 593
Hours : 20
Minutes : 15
Seconds : 35
Milliseconds : 615
Ticks : 513081356156056
TotalDays : 593,844162217657
TotalHours : 14252,2598932238
TotalMinutes : 855135,593593427
TotalSeconds : 51308135,6156056
TotalMilliseconds : 51308135615,6056
You can build more or less any format you like using the DateTime Format Strings and either the format operator (-f) or the ToString() method. For example:
(Get-Date).ToString("yyyy-MM-dd hh:mm:ss")
Which gives something like this:
2018-12-20 12:50:53
If you want to go the other way, you can do it like this:
Get-Date "2017-05-05 12:03:02 -0400"
This converts your string to an actual DateTime object, which can be directly compared to any other DateTime. So, to check if your target date is before now, for example, do something like this:
(Get-Date "2017-05-05 12:03:02 -0400") -lt (Get-Date)

How to convert date from one format to another? Need to get date to "Tue Apr 3 22:10:06 2018" format

How to convert "2018-04-03 22:10:06" to "Tue Apr 3 22:10:06 2018"? Obviously not those specific dates but that format.
I found this solution:
How to convert date format from dd/MM/YYYY to YYYY-MM-dd in swift
but I am unable to get it to the exact format.
So, I just threw this into Playgrounds
let inFormat = DateFormatter()
inFormat.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = inFormat.date(from: "2018-04-03 22:10:06")
let outFormat = DateFormatter()
outFormat.dateFormat = "E MMM d HH:mm:ss yyyy"
outFormat.string(from: date!)
It eventually outputs Tue Apr 3 22:10:06 2018
The formats were initial referenced from nsdateformatter.com
This, of course, will use the current system TimeZone and Locale, so you may want to do some more investigation into that

Localising DD MMM YYYY - How to test locally

I have a date in a format:
var date = "21 Sep 2017 14:00"
I want to change this date into en-US (in other words local) without the timezone. Which i guess should be:
"Sep 21 2017";
When i do this (I thought i would need to tell moment what the format of my date was):
moment.utc(date).local().format('DD MMM YYYY') it outputs "21 Sep 2017"
but if i do:
moment.utc(date).local().format() it still outputs "21 Sep 2017"
To test, I have been changing my regional settings from en-GB to en-US and it seems to make no difference.
What am i doing wrong here?
How do i convert the date to the local setting (and test it locally too)
I'm in en-GB
EDIT:
Re comments - Why then does this not say Set rather than Sep:
http://jsfiddle.net/rLjQx/5744/
As docs states:
By default, Moment.js comes with English (United States) locale strings. If you need other locales, you can load them into Moment.js for later use.
So first of all be sure that you are loading all required locales (see Loading locales in the browser or Loading locales in NodeJS).
Then you have to use locale() method to change locale of a moment object, local() is a different function. Note that moment usually uses 2 digit local code, so if you want to set locale to italian you have to use 'it' instead of "it-IT". You can find a full list of supported locales here.
Finally, since your input is not in a format recognized by moment(String) (ISO 8601 or RFC 2822), you have to use moment(String, String), as Matt Johnson highlighted in the comments.
Here a live example:
// var date = "21 Sep 2017 14:00";
// moment.utc(date, 'DD MMM YYYY HH:mm').local().format('DD MMM YYYY');
var m = moment("21 Sep 2017", 'DD MMM YYYY');
var formatted = m.locale("it").format("DD MMM YYYY");
$("#TestIT").text(formatted);
formatted = m.locale("en").format("DD MMM YYYY");
$("#TestEN").text(formatted);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment-with-locales.min.js"></script>
<div id="TestIT"></div>
<div id="TestEN"></div>

How to get last day of specified month in powershell

I need to get the last day of a month previous from specified.
I have a string (file name), which contains a date in its end. I need to capture the date (already done) and get the last date of the preceding month. For example the string is "proemail vytvoreni_9.2017 2017-10-16", so I need to get 30th September 2017. This is what I have now:
$Report = Read-Host "File name"
$Date = [datetime]$Report.Substring($Report.get_Length()-10)
$Last_month = $Date.AddMonths(-1)
$Date_text = $Last_month.ToString().Substring(3,7)
$month_year = ($Date_text.Split("."))
$days_count = [datetime]::DaysInMonth($month_year[1],$month_year[0])
$days_count = $days_count.ToString()
$month = $month_year[0]
$year = $month_year[1]
$Date_limit = [DateTime]($month,$days_count,$year)
All works well, except for the last row, that returns this error: Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.DateTime". I tried to convert $month and $year to string by .ToString() method, but it didn't help
(Get-Date).AddDays(-$(Get-Date).Day)
Saturday, September 30, 2017 2:36:19 PM
((Get-Date).AddDays(-$(Get-Date).Day)).Day
30
$filename = "proemail vytvoreni_9.2017 2017-10-16"
# Take the date from the filename
$sub = $filename.Substring($filename.Length-10)
# make it into a date format
$filedate = Get-Date -Date $sub
# take that date 2017-10-16 and substracts its own days so in this case substract 16 days, then show me the date
(($filedate).AddDays(-$filedate.Day)).Date
Output:
Saturday 30 september 2017 0:00:00
I've looked at this problem and different solution for this for awhile and here is my answer.
Create any date you want. Here is an easy way to get the month you want.
$date = [datetime]::Parse("11/2019")
Then simply add the next month and remove one day. p
$date.AddMonths(1).AddDays(-1)
Hope this helps somebody else.