I am not able to parse the date in the following format 'February 4, 2020, 3:15:14 PM GMT-6'
I tried to specify format but no luck.
With no format specified I get the warning:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format.
How do I get a parsed date from this string?
Thanks.
Unfortunately, Moment doesn't have a parsing token for offsets like GMT-6. The offset must be at least two digits to work correctly with the Z token. You can use a regex replace to alter your string before parsing.
var input = 'February 4, 2020, 3:15:14 PM GMT-6';
var adjusted = input.replace(/(GMT\+|-)([1-9])/, '$10$2');
var m = moment.parseZone(adjusted, 'MMMM D, YYYY, h:mm:ss A [GMT]Z');
m.format() //=> "2020-02-04T15:15:14-06:00"
(There are probably improvements to the regex that could be made, but this one works.)
Related
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
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>
I am trying to use Date class which is provided from Swift 3 library. I am not sure if I am doing it right.
When I print Date it prints correct date, but when I try to convert it from date to string it changes the date to something else.
let today = Date()
print(" Date object : \(today)")
let format = DateFormatter()
format.dateFormat = "mm/dd/yy"
print(" Date to String : \(format.string(from: today)")
Which gives the output:
Date object : 2017-06-03 18:13:39 +0000
Date to String : 13/03/17
mm is the format specifier for minutes, hence why the output returns 13 instead of 06, which is the time in minutes at which you called Date().
You'll need to use MM to display the month.
See the unicode report on date specifiers for more information: http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns
No it doesnt.
Your format is Minute, Day and Year.
Works exactly as it should.
Try MM istead of mm.
I'm reading an Excel spreadsheet using JXL and Groovy like this:
WorkbookSettings settings = new WorkbookSettings();
settings.encoding = "Cp1252"
settings.locale = new Locale("pt", "BR")
Workbook workbook = Workbook.getWorkbook(is, settings)
Sheet sheet = workbook.getSheet(0)
And then I have a cell in Excel which value is 09/01/2013 (dd/mm/yyyy). But then, when I retrieve cell contents, JXL automatically does some conversion and gives this back at me:
"09/01/13" == sheet.getCell(col, line).contents?.trim()
But then, when I cast the Cell to a DateCell, the Date representation of "09/01/13" becomes Jan 8, 2013 (!):
DateCell dc = ((DateCell) sheet.getCell(col, line))
println "date from JXL: ${dc.date}" // prints Tue Jan 08 22:00:00 BRST 2013
Would anyone have any ideas about how to fix this? If I could just retrieve the actual cell contents directly (09/01/2013), then I could do all the conversion stuff by myself.
Thanks!
From: http://www.andykhan.com/jexcelapi/tutorial.html#dates
When displaying dates, the java.util package automatically adjusts for the local timezone. This can cause problems when displaying dates within an application, as the dates look as if they are exactly one day previous to that which is stored in the Excel spreadsheet, although this is not in fact the case.
...
The easiest way to work around this (and the method used internally by the getContents() method of a jxl.DateCell) is to force the timezone of the date format as follows:
TimeZone gmtZone = TimeZone.getTimeZone("GMT");
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
format.setTimeZone(gmtZone);
DateCell dateCell = ....
String dateString = format.format(dateCell.getDate());
I have the following code
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
And the alert box shows the date
09/04/2014
instead of
21/04/2013
Why?
As others already say, it's because of your pattern. What they don't say is why it behaves that way.
When parsing 21/04/2013 as MM/dd/yyyy, DateTimeFormat will decompose the date as:
Month Day of month Year
21 4 2013
and it'll then adjust things to make a valid date. To do that, the Month part is truncated at 12 (so that temporary date is Dec 4th, 2013) and the remainder (21 - 12 = 9) is then added, leading to Sept. 4th 2014, which according to your format displays as 09/04/2014.
You wanted to show 21/04/2013 but the format was MM/dd/yyyy.
It should be dd/MM/yyyy
So change it like this:
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
You're reversing day and month.
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));