Powershell Convert DateTime to dd mon yyyy - powershell

I want to convert a date to this format "dd Mon yyyy".
I have this code which works:
$date = [DateTime]::Parse("21/11/2014")
$dateFormatted = $date.GetDateTimeFormats()[12]
#$dateFormatted displays 21 November 2014
Is there a way to convert it using something like this?:
$dateFormatted = $date.ToString("dd Mon yyyy")
At the moment this returns "21 11on 2014"

I worked it out:
$dateFormatted = $date.ToString("dd MMMM yyyy")

Related

in flutter dart convert 1624275605667 into Mon Jun 21 2021 17:10:05 GMT+05:30

var date = 1624275605667;
final DateTime formatted = DateTime(date);
final DateFormat fr = DateFormat('EEE MMM d yyyy HH:mm:ss');
final String dd = fr.format(formatted);
I try like this but getting some type of errors.
I want to convert 1624275605667 into this format Mon Jun 21 2021 17:10:05 GMT+05:30
For this which format I use here
DateFormat('EEE MMM d yyyy HH:mm:ss zzz')
Please try this one
var date = 1624275605667;
final DateTime formatted = DateTime.fromMillisecondsSinceEpoch(date);
final DateFormat fr = DateFormat('EEE MMM dd yyyy HH:mm:ss');
final String dd = fr.format(formatted);
print(dd);
You are using z pattern and it's not implemented yet. Issue is still open since 2015 https://github.com/dart-lang/intl/issues/19
And in intl package already mentioned that this characters are reserved and currently are unimplemented.
For workaround you can use
formatted.timeZoneOffset.toString(); /// 5:30:00.000000
Which is same as GMT+05:30

Swift "date from string" conversion doesn't work but the format works with online service?

Code for playground:
var dateStr = "Fri, 03 Aug 2018 08:55:22 GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.setLocalizedDateFormatFromTemplate("EEE, dd MMM yyyy HH:mm:ss z")
let date = dateFormatter.date(from: dateStr)
In the same time I use the online service - it shows that the format I chose is correct. Where is my mistake?
Small tip when you are working with DateFormatter, if it doesn't work one way, then do it the other way:
let string = dateFormatter.string(from: Date())
print("string: \(string)")
This way you'll see how your format is really interpreted.
If you do so, you'll get:
$> string: Fri, Aug 03, 2018, 12:05:09 GMT
See the extras , and the Aug 03 vs 03 Aug?
Now, your issue is that you misunderstood localizedDateFormatFromTemplate.
In fact you could remove spaces, punctuations and order.
For instance, dateFormatter.setLocalizedDateFormatFromTemplate("ddyyyyHHmmsszEMMM") gives the same output.
You just needs to give it what kind of infos you want (day, year, months, etc.), and it create the correct dateFormat corresponding in the localized version (adding then extra punctuations, etc.).
So use instead:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
Side note:
I currently live in GMT+2, so I added dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) to make it work.
You have to give dateFormatter.dateFormat to convert the string to date format. From the comment you get the answer but for others i am posting a correct answer here.
var dateStr = "Fri, 03 Aug 2018 08:55:22 GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z" //Added this line.
let date = dateFormatter.date(from: dateStr)

Which date format is this NSDate dateFormat?

I want to convert this string to NSDate,
Mon, 21 Mar 2016 10:26:45 GMT
so I set the date format to this:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
but it returns nil.
I tried to change this format to:
E, dd MMM yyyy HH:mm:ss zzz
EEE, dd MMM yyyy hh:mm:ss zzz
EEE, d MMM yyyy HH:mm:ss zzz
but it also returns nil.
Which date format should I use?
Your format string is ok but you have to add a compatible locale to the formatter.
For example your string works well with the US locale:
let source = "Mon, 21 Mar 2016 10:26:45 GMT"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let result = dateFormatter.dateFromString(source)
Try this code block :
let dateFormatter = NSDateFormatter()
let dateString = "Mon, 21 Mar 2016 10:26:45 GMT"
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let date = dateFormatter.dateFromString(dateString)
Try escaping the comma:
E',' dd MMM yyyy HH:mm:ss zzz
You can try the following code
let dateString:String = "Mon, 21 Mar 2016 10:26:45 GMT"
let dateFormat = NSDateFormatter.init()
dateFormat.dateStyle = .FullStyle
dateFormat.dateFormat = "ccc, dd MM yyyy HH:mm:ss zzz"
let date:NSDate? = dateFormat.dateFromString(dateString)
print(dateFormat.stringFromDate(date!))
For more info please visit NSDateFormatter formatting

Date format to show month as text

Struggling with this one. I'm trying to get the below to display like this: 19 Nov 2014
I suppose the real issue here is that I can't find any documentation to get the month to be displayed as text because if I could even get "19 November 2014" then I would just truncate it.
$prevWD = (date)+"$(-1+$(#(1,2-eq7-(date).dayofweek)))"
$prevWD.ToString("dd MM yyyy")
$date = Get-Date -Format "dd MMM yy"
See MS Documentation: Link
Can be done like this:
[datetime]::now.toString("dd MMM yyyy")
Or if you want yesterday:
[datetime]::now.addDays(-1).toString("dd MMM yyyy")
You can try this:
$dt = "{0:d MMM yyyy}" -f (get-date)
write-host $dt

formatting date from twitter response Zend Framework

im trying to formatting the date field 'created_at' from Twitter API response with Zend_Date. I want output the date like this:
21 of July of 2009, 12:30:00 (for example)
What format is this?:
Fri Oct 23 15:47:42 +0000 2009
thanks a lot
I've had the best luck just doing
$d = new Zend_Date(strtotime($input));
$twitter_format_out = $d->toString('EEE MMM dd HH:mm:ss Z YYY');
These date are not looking a standard format. Therefore, you have to create a format with the right constants (see them here).
Your first example (21 of July of 2009, 12:30:00):
$format = "d ' of ' MMMM ' of ' YYYY, h:mm:ss";
Your second example (Fri Oct 23 15:47:42 +0000 2009):
$format = "EEE MMM d h:mm:ss Z YYYY";
This formats you can use both for importing a date
$date = new Zend_Date($string, $format);
Or for outputting
$date->toString($format);
Look in the manual for locale support etc.