I am working on sample react-native app.
I am using moment.js to convert time to AM and PM representations.
My data is:
{
"start_date":"2017-09-29T18:29:59.000Z",
"end_date":"2017-09-29T19:29:59.000Z"
}
When converting the start_date and end_date to 12 hour clock format (AM/PM) using moment().format('LT'), I get the wrong time i.e. 11:59 PM for start_date - 12:59 AM for end_date.
How do I get the right time and format?
The comments are correct, since you are passing 2017-09-29T18:29:59.000Z it will turn to 11:59pm under your locale.
But LT as a format will really just return the time. Try using MM-DD-YYYY hh:mm:ss a for the format string.
You can check more here at the moment docs
I also made fiddle for you to play around with.
Related
I'm working on an app where users can choose what time zone they are recording time in. But internally, I want to store the time as a unix timestamp.
Say I have a date '2016-01-01 1:00 pm'. If I store this date in CDT (America/Chicago), it is my understanding that the resulting unix time stamp should be different than had I stored the same date in MDT (America/Denver). However, I get the same unix time stamp for both dates, and I'm having trouble understanding why.
var chicagoDate = moment('2016-01-01 1:00 pm').tz('America/Chicago');
var denverDate = moment('2016-01-01 1:00 pm').tz('America/Denver');
chicagoDate.format();
// "2016-01-01T07:00:00-06:00"
denverDate.format();
// "2016-01-01T06:00:00-07:00"
So far this confirms that the 2 dates are distinct from one another as shown by their UTC representation obtained by using format(). But here is where I'm getting confused:
chicagoDate.unix();
// 1451653200
denverDate.unix();
// 1451653200
Shouldn't 2 distinct UTC dates result in 2 distinct unix timestamps?
In:
moment('2016-01-01 1:00 pm').tz('America/Chicago');
there is no parse format parameter, so moment.js looks to see if the format is one that it can parse without the format. It can't, so it falls back to the built–in parser as if you'd written:
moment(new Date('2016-01-01 1:00 pm')).tz('America/Chicago');
And puts warning in the 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…
Safari at least parses it as an invalid date (timestamp), so you should include the parse format:
moment(new Date('2016-01-01 1:00 pm', 'YYYY-MM-DD h:mm a')).tz('America/Chicago');
Secondly, the string is parsed as local since there is no timezone associated with the timestamp. So both lines of code produce exactly the same time value (i.e. offset from the epoch) and the two Dates produced are effectively identical.
The call to tz sets the timezone to use for all subsequent operations, so the output shows different times because different offsets have been applied at the formatting stage. The actual Dates being formatted are still effectively identical.
If you want the string to be parsed as for another timezone, you need to either manually include it in the string at the parse stage (e.g. '-06:00' or whatever), or use a library like Luxon with an IANA location (e.g. 'America/Chicago').
How to format this string to date and time using Momentjs?
01/08/2018 12:00 AM.
I am thinking that it has to be converted to GMT for the time to work. Though I have seen many cases on SO but many of them only extracted the date leaving the time.
This is an extremely simple task with MomentJS. Try this one:
let x = moment("01/08/2018 12:00 AM").format('DD/MM/YYYY hh:mm A');
alert(x);
Example in JsFiddle: https://jsfiddle.net/5kf6afme
8:53 PM PDT - basically 12hrs format with AM/PM.
Irrespective of machine time format.
I suggest using moment Timezone: http://momentjs.com/timezone/
then it's as simple as
moment().tz('PDT').format('hh:mm A z')
in my ASP.net application, in the js, i got the date format as '2017-04-26T09:00:00Z'. What is this format? And when i return this to the view page, the date is changed to 26/04/2017 02:00 am. But the actual time is 09.00 am. Kindly help me why this is happening? My js code is
{
"data": "Date",
"render": function (data) {
return moment(data).format('MM/DD/YYYY H:m');
}
}
'Z' stands for Zulu time, which is also GMT and UTC.
moment is converting that timestamp (your variable data) to your local timezone which appears to be 7 hours behind GMT.
Based on W3scholls :
Date and time is separated with a capital T
UTC time is defined with a capital letter Z
And if you test this link you will be get the secret behind this.
The date you see in the JS is actually your the date Time with your timezone. On server side it will automatically get converted to the UTC timezone.
you must be at UTC + 7:00Hrs time zone right.
If you want to get the Date right just strip the Time part in JS,
moment(data).format('MM/DD/YYYY');
I have an API call that receives a date string like 2016-2-12 //('YYY-MM-DD').
I am using moment-timezone, but not having any luck.
The end goal is to get the unix timestamp for midnight 2016-2-12 of the timezone of the applicable site (not the server)
I have tried a lot of combinations of something like:
moment.tz('2016-2-12', 'America/New York').utc();
But it seems to receive the input date as UTC (my server is set to UTC) then do the timezone conversion AFTER, giving me a date for the day before.
For reference I am generating a report for a date range. So if the input is:
&start=2016-4-12&stop=2016-4-15 //and the timezone is America/New York
I expect a start time of 2016-4-12T00:00:00 -04:00 but i keep ending up with dates from the day before.
I expect the function to be something like:
moment.tz('2016-2-12', 'YYYY-MM-DD', 'America/New York').startOf('day').utc();
As soon as i asked the question, after hours of screwing around, i got it.
moment.tz('2016-4-14', 'YYYY-MM-DD', 'America/New York').format();
is yielding 2016-04-14T00:00:00-04:00
Sorry to waste time here... I hope someone else finds this helpful...
moment.tz('2016-4-14', 'YYYY-MM-DD', 'America/New York').endOf('day').format();
is yielding 2016-04-14T23:59:59-04:00 as well and i can easily get unix timestamp using utc()