How can i use moment.js in both: Australia & USA time formats?
For example:
07/08/2017 - is good for both time formats, but!
30/08/2017 - is invalid for moment.js, but i can have such dateTime
You can check it here:
http://jsfiddle.net/rLjQx/2135/
The parser is assuming that digits of the form XX-XX-XXXX are representing DD-MM-YYYY. If you'd like it to accept MM-DD-YYYY then you need to specify this.
eg var now2 = moment('08/30/2017', 'MM-DD-YYYY').format('MMM DD h:mm A');
You can also specify an array of different formats that you'd like it to accept so that it will recognise both:
var now2 = moment('08/30/2017', ['DD-MM-YYYY', 'MM-DD-YYYY']).format('MMM DD h:mm A');
Specify the format via a second parameter to the moment call
var now2 = moment('30/08/2017', 'DD/MM/YYYY').format('MMM DD h:mm A');
Otherwise there is no way for moment to know
Related docs here: https://momentjs.com/docs/#/parsing/string-format/
Corrected fiddle: http://jsfiddle.net/wu6wwsvp/
In your fiddle you are using a very old version of moment (2.2.1), I suggest to upgrade it to lastest one (2.18.1).
Using a newer version, you will have a Deprecation Warning in your console:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Following the link (and moment(String) docs) you will discover that you have to specify format to parse you string correctly.
As Billy Reilly suggested you can use moment(String, String[]) parsing function. Please remeber that:
Starting in version 2.3.0, Moment uses some simple heuristics to determine which format to use. In order:
Prefer formats resulting in valid dates over invalid ones.
Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
Prefer formats earlier in the array than later.
So the way 07/08/2017 will be interpreted depends on the order of the format in array of formats parameter.
Here a snippet with some examples:
var now = moment('30/08/2017', ['MM/DD/YYYY','DD/MM/YYYY']);
var now2 = moment('08/30/2017', ['MM/DD/YYYY','DD/MM/YYYY']);
var now3 = moment('07/08/2017', ['MM/DD/YYYY','DD/MM/YYYY']);
console.log(now.format('MMM DD h:mm A')); // Aug 30 12:00 AM
console.log(now2.format('MMM DD h:mm A'));// Aug 30 12:00 AM
console.log(now3.format('MMM DD h:mm A'));// Jul 08 12:00 AM
var now4 = moment('30/08/2017', ['DD/MM/YYYY','MM/DD/YYYY']);
var now5 = moment('08/30/2017', ['DD/MM/YYYY','MM/DD/YYYY']);
var now6 = moment('07/08/2017', ['DD/MM/YYYY','MM/DD/YYYY']);
console.log(now4.format('MMM DD h:mm A')); // Aug 30 12:00 AM
console.log(now5.format('MMM DD h:mm A')); // Aug 30 12:00 AM
console.log(now6.format('MMM DD h:mm A')); // Aug 07 12:00 AM
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Related
I am developing a Node.js application and I have to convert a German string date like am 13. Dezember 2017 to ISO Date and when I used moment.js library to convert it I got an invalid date, any solutions?
You can parse 13. Dezember 2017 using moment(String, String, String) and then use toISOString().
Since your input is neither in ISO 8601 recognized format, neither in RFC 2822 you have to provide format parameter. DD stands for day of the month, MMMM stands for month's name and YYYY stands for 4 digit year.
The third parameter tells moment to parse input using given locale:
As of version 2.0.0, a locale key can be passed as the third parameter to moment() and moment.utc().
Note that you have to import de locale to make it work (using moment-with-locales.js or /locales/de.js in browser or following Loading locales in NodeJS section for node).
Here a live example:
var m = moment('13. Dezember 2017', 'DD MMMM YYYY', 'de');
console.log( m.toISOString() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment-with-locales.min.js"></script>
You can use the toISOString method, it return a Date object as a String, using the ISO standard:
var d = new Date();
var n = d.toISOString();
Have a look here:
https://momentjs.com/docs/
Do you need something like that:
moment('13. Dezember 2017', 'DD. MMMM YYYY', 'de').format(moment.ISO_8601);
You can easily format it using:
var now = moment(); // pass your date to moment
var formatedNow = moment().toISOString(now);
or
var formatedNow = now.toISOString();
I have this format date:
01Mar-0234
26Feb-0430
01 is day, mar or feb is mounth and 0430 is 4 oclock 30 in formater zulu +00.
I would like to use moment for converting this format, I'm trying this:
moment('26Feb-0430').format("DD-MM-YY HH:MM");
but I haven't good format and I have this error :
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment c
onstruction falls back to js Date(), which is not reliable across all browsers and version
s. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major r
elease. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
Can you help me for have 26/02/2017 06:30 for summer and 5h30 winter?
As suggested in the comments and in the warning message you have to use moment(String, String) parsing function.
In the format string parameter you have to use moment tokens where: DD is day of the month. MMM is month's short name, HH is 0-23 hours and mm (lowercase) is minutes.
Since you have to threat your input as +00:00, you have to use moment.utc.
Use format() to display the parsed moment object passing the tokens you need, always remember that moment tokens are case sensitive.
Here a working sample:
var result = moment.utc('26Feb-0430', 'DDMMM-HH:mm').format("DD/MM/YYYY HH:mm");
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
i am using nodejs to connect to mySql and connection works.
while using query such as
SELECT * from someTable
i get the required results. But the problem is that i am having
a column which stores date is having the given format.
before sending the data i need this format to be converted and vice versa
file_date : Wed Jan 01 2014 05:34:53 GMT+0530 (IST)
This is format i am getting
i am unable to send params with Date in this format from client side
so i need to convert this data to "DD-MM-YYYY" format.i need this way so that i can pass params in "DD-MM-YYYY" and fetch data date wise
Simple Approach
If you know your date format is supported in Moment, then the simplest approach is to construct a moment object and format it.
var file_date = 'Wed Jan 01 2014 05:34:53 GMT+0530 (IST)';
var formatted = moment(file_date).format('DD-MM-YYYY');
Deprecation Warning:
However, at some point support for non ISO date formats was deprecated. It will in some cases still work but you should be warned (per the deprecation warning message):
Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release.
Workaround
Instead, if you know the format of an input string, you can use that to parse a moment.
var s = 'Wed Jan 01 2014 05:34:53 GMT+0530 (IST)';
var m = moment(s, 'ddd MMM DD YYYY hh:mm:ss [GMT]ZZ').format('MM-DD-YYYY');
console.log(m);
<script src="https://momentjs.com/downloads/moment.min.js"></script>
When specifying a format manually like this, it helps to have the format documentation handy: https://momentjs.com/docs/#/displaying/format/
Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven't had any success.
Is there a way using moment.js to parse an ISO-8601 string but have it parsed in my local timzeone? Essentially I want to ignore the timezone information that is supplied in the ISO string.
For example, if I am in EDT timezone:
var x = moment( "2012-12-31T00:00:00+0000" );
will give me:
"2012-12-30T19:00:00-5000"
I'm looking to ignore the timezone info and just have it give me a moment equivalent of "2012-12-31T00:00:00-5000" local time (EDT).
I don't think you really want to ignore the offset. That would ultimately just be replacing the offset you provided with one from your local time zone - and that would result in a completely different moment in time.
Perhaps you are just looking for a way to have a moment retain the time zone it was given? If so, then use the moment.parseZone function. For example:
var m = moment.parseZone("2012-12-31T00:00:00+0000");
var s = m.format(); // "2012-12-31T00:00:00+00:00"
You could also achieve this with moment.utc. The difference is that moment.parseZone will retain whatever offset you give it, while moment.utc will adjust to UTC if you give it a non-zero offset.
I solved this by supplying a format as the second argument, and using Moment's method of escaping characters, and wrapped square brackets around the timezone.
moment("2016-01-01T05:00:00-05:00", "YYYY-MM-DDTHH:mm:ss[Z]").startOf("hour").format()
This will still create moment objects using your local time zone, but it won't do any sort of auto-timezone calculation. So the above example will give you 5am regardless of timezone supplied.
I know I'm late to the party, I had the same question and my searches didn't bring me any closer. I broke down and read the documentation and there is an option in moment for a String + Format:
String + Format docs
moment(String, String);
moment(String, String, String);
moment(String, String, Boolean);
moment(String, String, String, Boolean);
and more words, then this:
Unless you specify a time zone offset, parsing a string will create a date in the current time zone.
moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
The part that gave me pause was the example that was used to parse local time omitted the +0000, which lead me to think the input string needed to have that removed, but it doesn't.
example:
var time = "2012-12-31T00:00:00+0000";
var x = moment(time); // Sun Dec 30 2012 19:00:00 GMT-0500
var y = moment(time,'YYYY-MM-DD'); //Mon Dec 31 2012 00:00:00 GMT-0500
You can ignore the browser's timezone completely by creating a new moment using moment.utc() instead of moment().
For example, if you are trying to work purely with a UTC date/time of the browser's current time but want to discard its timezone data, you can recreate the browser's current time into a UTC format using the following:
let nowWithTimezone = moment();
let nowInUtc = moment.utc(nowWithTimezone.format('MM/DD/YYYY HH:mm'), 'MM/DD/YYYY HH:mm');
Further documentation on moment.utc(): https://momentjs.com/docs/#/parsing/utc/
If you know for sure your input string is in the ISO-8601 format, you could just strip off the last 5 digits and use that in the Moment constructor.
var input = "2012-12-31T00:00:00+0000"
input = input.substring(0, input.length-5)
moment(input).toString()
> "Mon Dec 31 2012 00:00:00 GMT-0600"
There are valid reasons to do what the OP is asking for. The easiest way to do this with Moment is using its parseZone(date) method. No futzing around with string manipulation or multiple calls. It effectively parses the date string as though it were in the browser's local time zone.
This is difficult task to do with MomentJS, it will basically depend as well on your current timezone.
Documentation as well is vague for this specific task, the way I solved the issue on my side was by adding hours to the date before converting it to JSON format.
var dt = moment("Sun Sep 13 2015 00:00:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400", false);
var date = dt.add(2, 'hour').toJSON();
console.log(date); //2015-09-13T00:00:00.000Z
Momentjs default logic will format the given time with local timezone. To format original date, I wrote a function:
https://github.com/moment/moment/issues/2788#issuecomment-321950638
Use moment.parseZone to convert without taking into account the timezone.
const moment = require('moment')
const dateStr = '2020-07-21T10:00:00-09'
const date = moment.parseZone(dateStr)
console.log(date.format('MM-DD-YY HH:mm A')) // 07-21-20 10:00 AM
Try here link to docs
The best way is to use:
dt = moment("Wed Sep 16 2015 18:31:00 GMT-0400", "ddd MMM DD YYYY HH:mm:ss GMT-0400",true);
And to display convert again to desired timezone:
dt.utcOffset("-04:00").toString()
output > Wed Sep 16 2015 18:31:00 GMT-0400
I am using moment.js library
I have a date in this format :
2014-08-07T10:00:00+02:00
I want to have two separate values :
- Thursday, August 7 2014
- 10 am
But I also want them to be using the local language.
For example, if moment.lang("fr"), the output should be
- Jeudi 7 Août 2014
- 10h
I set the moment.js lang in the correct way.
I managed to remove hour,minutes and seconds (to extract the first value) :
new Date(moment.utc(date).format('LL')) //Outputs Thu Aug 07 2014 00:00:00 GMT+0200 (Paris, Madrid)
But I don't know how to extract the hour and minutes (for the second value) and how to show the date using the current language.
#Rayjax solution is not working anymore in recent versions of moment.js. Behavior of moment.localeData().longDateFormat() changed. LT is now already replaced by time format. Instead of dddd, MMMM D, YYYY LT it returns now dddd, MMMM D, YYYY h:mm A. Therefore removing LT from string does not work anymore. But we could just remove compiled LT for current locale:
moment.localeData().longDateFormat('LLLL')
.replace(
moment.localeData().longDateFormat('LT'), '')
.trim();
Trim is necessary to avoid unnecessary white spaces.
Hi I don't know if it's the best way do to it but it's working
moment.locale("en");
var now = moment()
console.log(now.format(moment.localeData().longDateFormat('LLLL').replace('LT' , '')));
console.log(now.format(moment.localeData().longDateFormat('LT').replace('mm' , '').replace(':' , '').replace('.' , '')))
http://jsfiddle.net/yann86/tb0u5eav/
I came up with this which works well :
moment.lang("fr"); //en, es or whatever
var date = moment(dateParam).format("LL"),
time = moment(dateParam).format("LT");
console.log(date, time) -- outputs separatedly date and time in the correct language form
What I was missing were the language configs files : http://momentjs.com/ i grabbed momentjs+locales instead of moment.js only and it worked.
It is important to note that moment.js won't tell you you are missing those filed, it will just default to english.
It worked for my needs:
moment().locale('en').format('L');
moment().locale('pt-br').format('L');
Just change the format for your needs. The Documentation of the moment is great. http://momentjs.com/