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>
Related
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.
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
Using Swift 4, is there a way to get a string representation of a locale's date format? Based on an iPhone's locale settings, I would like to find out if the format is yyyy/mm/dd or mm/dd/yyyy.
I've found all of the ways for changing a date format or getting the date from the locale's format but have not been able to work out how to get the actual format.
Use DateFormatter dateFormat(fromTemplate:options:locale:).
let userFormat = DateFormatter.dateFormat(fromTemplate: "yyyyMMdd", options: 0, locale: Locale.current)
In the US this returns MM/dd/yyyy. In Germany this gives dd.MM.yyyy.
If you ever need to get the appropriate time format which also takes into account the user's chosen 12/24-hour time format, use a template of jms. The j is a special format specifier, only used with templates, that returns either h or H for the hour depending on what's appropriate.
Another possible option is to create a DateFormatter, set the desired dateStyle and timeStyle, then read the dateFormat property:
var mydf = DateFormatter()
mydf.dateStyle = .long // set as desired
mydf.timeStyle = .full // set as desired
print(mydf.dateFormat)
For the US this gives:
MMMM d, y 'at' h:mm:ss a zzzz
For Germany this gives:
d. MMMM y 'um' HH:mm:ss zzzz
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
I have a string of the format Jan 25, 2011 3:17 AM. I need to convert it to NSDate.
I used NSDateFormatter with format #"MMM d, yyyy h:mm a". It Works well if iphone time is in 12 hr format, but returns nil if time is in 24 hr format. Can any one help me with this????
Capital H is used for 24 hour format. Don't 24 hour times usually exclude the AM/PM part? If so, your format string should be: #"MMM d yyyy H:mm".
Here's a reference for Unicode date format strings.
It's a bug in NSDateFormatter. You can work around it by manually setting a locale on the date formatter:
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"] autorelease]];