parseexact translate everything into january in powershell - powershell

as you can see below, no matter what date i put in there , it gets translated into January.
anyone has an idea please?
thank you very much!
PS C:\Users\jy70606> [datetime]::ParseExact("20170519".trim(),'yyyymmdd',$null)
Thursday, January 19, 2017 12:05:00 AM
PS C:\Users\jy70606> [datetime]::ParseExact("20170219".trim(),'yyyymmdd',$null)
Thursday, January 19, 2017 12:02:00 AM
PS C:\Users\jy70606> [datetime]::ParseExact("20160119".trim(),'yyyymmdd',$null)
Tuesday, January 19, 2016 12:01:00 AM
PS C:\Users\jy70606>

mm is minutes. Use MM for months - E.g:
[datetime]::ParseExact("20170219".trim(),'yyyyMMdd',$null)
Check out this page on Formatting DateTime, and the Custom DateTime Format Strings page
Related: Standard DateTime Format Strings

Related

Java8 DateFormatter for pattern "Dec 01, 2019 1:00:00 PM +00:00" [duplicate]

This question already has answers here:
Parsing time-of-day with am/pm using Java DateTimeFormatter class [duplicate]
(3 answers)
Can't parse String to LocalDate (Java 8)
(2 answers)
Closed last month.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
I am trying to format the String date, time and zone information.
LocalDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, YYYY hh:mm:ss a XXX"));
ZonedDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, YYYY hh:mm:ss a XXX"));
Please note for project support reasons i cannot use above java 8.
I am unable to get this parse work, I did try a lot of versions before i posted here. any support is appreciated.
Exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text 'Dec 01, 2019 1:00:00 PM +00:00' could not be parsed at index 13
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
at com.parse.Test.main(Test.java:10)
Your date-time string, "Dec 01, 2019 1:00:00 PM +00:00" has timezone offset of 00:00 hours. The java.time API provides us with OffsetDateTime to parse this type of date-time string to.
Demo:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
class Main {
public static void main(String[] args) {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MMM dd, uuuu h:mm:ss a XXX", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", parser);
System.out.println(odt);
}
}
Output:
2019-12-01T13:00Z
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
What went wrong with your code?
You have used hh whereas your string has only one digit in the hour.
You have used Y instead of y. Note that Y is used for week-based-year. The right symbol for the intended purpose is y or u. Here, you can use y instead of u but I prefer u to y.
I also recommend you understand the importance of using a Locale while using date-time parsing/formatting API. Check Never use SimpleDateFormat or DateTimeFormatter without a Locale to learn more about it.
This Worked
LocalDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a XXX"));
ZonedDateTime.parse("Dec 01, 2019 1:00:00 PM +00:00", DateTimeFormatter.ofPattern("MMM dd, yyyy h:mm:ss a XXX"));
What I changed was:
We use lower case yyyy for the year. Format pattern strings are case sensitive.
We use just one h for the hour of day since it may be just one digit (1 in my example). A single h does accept two digits too, though, so times with 10, 11 or 12 will not pose any problem.

date gets increased by a day

I have a date in my mongodb database in following format
{
"_id" : ObjectId("5b8cd5e5cf36cb517c91910e"),
"date" : ISODate("2018-09-04T23:58:00.000Z")
}
When I show it by using moment on front end it gets increased by one day so 2018-09-04 becomes 2018-09-05
On front end I do this
moment(date).format('dddd MMM DD, YYYY')
I want same date extracted from the database that is 2018-09-04
Any help is appreciated.
Edit ->
When I console it on back end I see this
date: 2018-09-04T23:58:00.000Z
And on front end
date: "Thu Sep 06 2018 05:28:00 GMT+0530 (India Standard Time)"
Mongodb stores dates in ISO formats, it depends on where (in what timezones) your server and client are located. You can use the below.
The format for date you are getting is UTC format, when it will get converted to IST, it would show the difference. It means the following :-
If I get 2019-09-26T18:30:00.000Z as date in UTC. It is equivalent to Fri Sep 27 2019 00:00:00 GMT+0530 (India Standard Time). Please look at the difference 5hrs 30 mins. Incrementing/decrementing any thing on this and showing in IST would be wrong again as per the timezone is concerned.
If you do
new Date(date).toUTCString()
It will show you "Thu, 26 Sep 2019 18:30:00 GMT" In GMT which is exact.
It seems like a timezone issue.
If I run
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+00:00").format());
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+05:30").format());
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+00:00").format('dddd MMM DD, YYYY'));
console.log(moment('2018-09-04T23:58:00.000Z').utcOffset("+05:30").format('dddd MMM DD, YYYY'));
I get
2018-09-04T23:58:00Z
2018-09-05T05:28:00+05:30
Tuesday Sep 04, 2018
Wednesday Sep 05, 2018
Which is off by one day

How to change freemarker date value?

I am getting {item.pubDate} from XML and the value is:
Mon, 02 Mar 2015 14:35:47 +0000
so I did this:
<#assign starting_point = item.pubDate?index_of(",")>
<#assign date="${item.pubDate?substring(starting_point + 1)}" />
${date?datetime("dd MMM yyyy hh:mm:ss z")?date}<br>
and the result is: Mar 2, 2015.
My question is, can we change value from Mar to March and if we can then what is the best way to do it? I could have if/elseif statements in freemarker and assign each three letter months to full month name but it looks not good. Any advice/tips will be greatly appreciated. thanks.
It doesn't mater, MMM will parse both Mar and March. The only important thing is to have at least 3 M-s, as http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html says:
If the number of pattern letters is 3 or more, the month is interpreted as text; otherwise, it is interpreted as a number.
Yes, FreeMarker follows the same datetime formatting rules as Java. Use the ?string built in for dates. You can do:
${date?datetime("dd MMM yyyy hh:mm:ss z")?string("MMMM dd, yyyy")}
Source: http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_string_for_date

How to use an abbreviated month with Zend_Date and locale formatting

I'm trying to set a long date format with an abbreviated month that uses the locale of the user. Here's what I have now.
//getDateTimeObject('field') grabs a Zend_Date object of the field
$creationDate = $this->getDateTimeObject('objectCreated');
$creationDate->get(Zend_Date::DATE_LONG)
That produces the appropriate dates. For en_US, "September 27, 2012". For en_GB, "27 September 2012". The problem is I need it to use the abbreviated month: en_US "Sept 27, 2012" and en_GB "27 Sept 2012".
How can I respect the locale and use an abbreviated month?
echo $creationDate->get(Zend_Date::DATE_MEDIUM);

Groovy date parse issue

date.parse() method of groovy detects date DD and year yyyy correctly but is unable to detect the month as mmm.. As in
println new Date().parse("DD-MMM-yyyy", '22-MAR-2011')
yields output as
Sat Jan 22 00:00:00 GMT+05:30 2011
Why is the month march as MAR picked up as Jan? What can I do to make it detect the month in mmm format?
The problem is actualy that you are using DD - that means day in year
Correct way:
println new Date().parse("dd-MMM-yyyy", '22-MAR-2011')
Quick tip when formatting dates try using the reverse and see what comes out:
println new Date().format("dd-MMM-yyyy")
Groovy uses SimpleDateFormat under the hood but that's not that important since most date libraries use the same format conventions.