Date format to show month as text - powershell

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

Related

Trouble with converting to Datetime object

I have the below data which is an object type variable named $timestamps
Sat Jan 15 16:21:24
Sat Jan 15 01:31:22
Fri Jan 14 20:58:09
Fri Jan 14 20:51:02
I'm having trouble converting it to Datetime object because of the weird date format. How would you handle this?
I would like it as a datetime object because I plan to convert from current (UTC) to EST.
TIA
You can use the the ParseExact() method provided by the [datetime] class for this:
[datetime]::ParseExact('Fri Jan 14 20:58:09','ddd MMM dd HH:mm:ss',$null)
# returns a - datetime - object of:
# Friday, January 14, 2022 8:58:09 PM
dd - for the day.
MM - for the month.
HH - for the hour - Capitalized for the 24 hour time format.
mm - for the minutes.
ss - for the seconds.
Edit: as suggested by mklement0, we can use [cultureinfo]::InvariantCulture to make the parsing specific to an English date time format. Also, changing dd to d as a more robust solution for days without 2 digits; which should cover both singular, and double digit days.
Seeing $timestamps is an array of strings, you can use a loop (of your choice - in this case the Foreach-Object cmdlet) to iterate through each string parsing the text to return a datetime object:
$timestamps | ForEach-Object {
$culture = [cultureinfo]::InvariantCulture
$format = 'ddd MMM d HH:mm:ss'
$date = [datetime]::ParseExact($_,$format,$culture,'AssumeUniversal, AdjustToUniversal')
[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($date, 'Eastern Standard Time')
}
Using 'AssumeUniversal, AdjustToUniversal' ensures a UTC output.
Assuming from your comment that you'd like to do a conversion to Eastern Time, passing the newly created datetime object to [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId() with an argument of the desired time zone, you can get your result in the new time zone.
When using $null, the CultureInfo object that corresponds to the current culture is used.
The DateTime.ParseExact() method is probably what you're looking for.
PS C:\TEMP>$timestamp = 'Sat Jan 15 16:21:24'
PS C:\TEMP>$format = 'ddd MMM dd HH:mm:ss'
PS C:\TEMP>[datetime]::ParseExact($timestamp, $format, $null)
Saturday, January 15, 2022 04:21:24 PM
PS C:\TEMP>

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

Identify dateformat matlab

I am looking for a way to identify the dateformat when adding a cellstr.
Thus when I run the function:
customFunction('2017/01/06')
that it returns
'yyyy/mm/dd'
How can this be achieved?
You could use java.text.SimpleDateFormatinside inside matlab function
import java.text.SimpleDateFormat;
sdf = SimpleDateFormat('yyyyMMdd')
sdf.parse( char('20180410') )
ans =
Tue Apr 10 00:00:00 IST 2018
You'll have to add try ... catch.. for all possible formats and loop over each.
"yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ssZ",
"yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss",
"MM/dd/yyyy HH:mm:ss", "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'",
"MM/dd/yyyy'T'HH:mm:ss.SSSZ", "MM/dd/yyyy'T'HH:mm:ss.SSS",
"MM/dd/yyyy'T'HH:mm:ssZ", "MM/dd/yyyy'T'HH:mm:ss",
"yyyy:MM:dd HH:mm:ss",
"yyyyMMdd"

Powershell Convert DateTime to dd mon yyyy

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")

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.