YYYY-MM-DDTHH:mm not getting am/pm momentjs - javascript

I know this type of question is not really apropiated, but I'm stuck on it and some help would be apreciated it. I have the following date:
2018-04-26T08:19:30+02:00
But when I try to get the Hours and Minutes, the result I get is:
20:19 p
What I'm doing is the following:
let momentDay = (moment(el.time,'HH:mm a').format('HH:mm a'));
I don't know how I could get the real time, the one should give me: 08:19 instead of 20:19
Thanks

You should use hh for twelve hour time, rather than HH, which is for 24 hour time.
Also, if el.time is a datetime object, or a recognizable unambiguous date format (as yours is), then you don't need to specify a format in the constructor:
let momentDay = moment(el.time).format('hh:mm aa');

Related

How to work with Moment.js to return time in a 24-hours format when adding to it?

I get hours as strings in my app, e.g. "2230". I would like to be able to add minutes to it, so as to simulate the time that it will be after those minutes have been added to it, e.g.
//"2230" + "60"mins = "23:30"
//"2230" + "180"mins = "02:30"
I read that Moment.js could be a solution to this, but I couldn't figure out:
what the right way to format the hours initially is with moment("2230").format()
how to add minutes to it
how to make it behave like a 24-hour clock when adding to it
Moment is a great tool for doing this. It takes some syntax tricks to get it right, but I think this is what you're looking for:
moment("2230", "HH:mm")
.add(60, "minutes")
.format("HH:mm")
Feel free to play around with it here:
https://codesandbox.io/s/proud-pine-lz0fs?file=/src/index.js
As you can see, as long as your time string is always 4 characters long, moment can extract the HH:mm format, then you can add minutes, and then format the final output to HH:mm again.
Here are 2 great resources:
https://techstream.org/Bits/Javascript/Javascript-Parse-time
https://flaviocopes.com/momentjs/
Hope that helps!
First you have to split this string to get the hours and minutes from it.
const s= "2230"
const hour = s.substring(0,2);
const min = s.substring(2,4);
After that you can easily pass this hours and min to a moment.
const time = moment()
.set({"hour": hour, "minute": min})
.add(60, 'minutes')
.format("HH:mm");
the .set is to set the time (hours minutes)
the .add is to add the minutes you wanted to add
the .format is to format the output as you pleased,
NOTE the capital "HH" means 24/h clock while "hh" means 12/h clock

How can I get moment.js to output a formatted time in UTC time with milliseconds?

var end, moment, timeFormat;
moment = require('moment');
end = moment.utc('2016-11-29T23:59:59.999');
console.dir(end.format());
timeFormat = 'YYYY-MM-DDThh:mm:ss.SSS';
console.dir(end.format(timeFormat));
Output:
'2016-11-29T23:59:59Z'
'2016-11-29T11:59:59.999'
What I really need:
'2016-11-29T23:59:59.999'
Why is there a 12 hour difference between those 2 outputs? I could just add 12 hours to it, but that is hacky. I don't understand why moment is suddenly subtracting 12 hours from my date as soon as I give it a format. Seems unlikely this is a bug; more likely me misunderstanding what Moment is doing.
My timezone is GMT+8.
Because you are using hh (01-12) instead of HH (00-23); see the Moment.js format() docs.
Here is a working example:
var end, wrongTimeFormat, timeFormat;
end = moment.utc('2016-11-29T23:59:59.999');
console.dir(end.format());
wrongTimeFormat = 'YYYY-MM-DDThh:mm:ss.SSS';
timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS';
console.dir(end.format(wrongTimeFormat));
console.dir(end.format(timeFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.0/moment.min.js"></script>
According to the Moment.js docs, lowercase hh will produce hours in the 01-12 range, which are meant to be used in conjunction with am/pm. You need capital HH (00-23, "military time"), as in
timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS'
use uppercase HH
timeFormat = 'YYYY-MM-DDTHH:mm:ss.SSS';

Turn a String to a 24 hour time format, using moment.Js

I am trying to format a String into a 24 hour format using moment JS at the moment I am not getting the expected output, the below explains it in more detail:
How I am coverting
var testFormat = moment("9:00","HH:mm:ss");
I was hoping this would output:
09:00:00 (24 Hour format)
But for some reason is does not convert just the date, this is outputing the following:
1471507200000
You are using it wrong. You can provide a format as the second argument to moment. So in your case it would be:
var myMoment = moment("9:00","H:mm");
To get the moment. Now you can format it the way you want:
myMoment.format("HH:mm:ss");
DEMO
moment("13:15:00", "h:mm:ss").format("hh:mm A");
it will give 12 hrs format

moment.js returning an unexpected fromNow date when date is MM/DD/YYYY HH:mm:ss

I have a date in the following format var timestamp = "6/9/2016 1:47:31 PM";. I'm trying to get the relative time (4 hours ago, 3 minutes ago, 3 days ago, etc...) from the timestamp compared to the current datetime using from now.
var LastReading = moment(timestamp).fromNow();
but this is returning "2010 years from now". I tried using the format
var LastReading = moment(timestamp, "MM/DD/YYYY HH:mm:ss").fromNow();
but I get the same result. Any ideas? Do I need to format the date in a different way in order to get the fromNow method to work as expected?
To match your timestamp, the format should look like this:
MM/DD/YYYY hh:mm:ss A
HH means 24 hour time, but you're using 12 hour time, for which you need to use hh. Also, A will match AM/PM.

How can I create a formatted date field in javascript

In Javascript I want to be able to create a string for a date. I would like to use the format
dd-MMM-yyyy
I would like the dd part to change between 1 and 29 every time I create the variable (I'm using a loop)
the MMM is set to Jan
the yyyy to 1999
Can someone help by giving me advice on how I can do this?
var formatted_date = Math.floor(Math.random() * 29) + 1 + '-Jan-1999';
console.log(formatted_date);
You can do several things.
Check out 10 ways to format date and time using Javascript.
Another set of examples here.

Categories

Resources