I'm using momentJS to format some dates for a chartJS implementation.
In the chart i'm predicting the next 15 minutes worth of data so need to add the next 15 minutess worth of datetimes to my chartLabel variable.
I use the below function to achieve this. The issue is that the formatting for every other datetime seems to reverse the day and month. I have no idea why this is happening, has anyone seen this before?
predictMarketData(){
let lastDate = this.marketData[this.marketData.length-1][0]
lastDate = moment.unix(lastDate).add(1, 'minutes').format('DD/MM h:mm a')
for(var _i = 0; _i < 15; _i++){
this.chartLabels.push(lastDate);
lastDate = moment(lastDate).add(1, 'minutes').format('DD/MM h:mm a');
}
}
It appears to add minutes correctly but the day month formatting appears to reverse every other, screenshot provided.
Any help would be much appreciated!
Related
I'm trying to use Moment JS and having issues adding or subtracting dates.
For example, I'm trying to sanity check right now and have this in code:
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = startMoment.subtract(1, "day");
^ this doesn't work and when I print future and start moment, they show the same time and date.
I tried also changing stuff around so that I create a new moment instead of using startMoment:
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A").subtract(1, "day");
But it's still not working. Am I just missing something?
Help would be greatly appreciated as I am new to this and have wracking my head for a while on why it's not working.
Watch the documentation closely, substracting or adding dates forces you to specify the chrono unit. You just wrote 'day', but its either 'days' or just simply 'd'.
let someDateString = "01/01/2000 12:00 AM";
let startMoment = moment(someDateString, "MM/DD/YYYY hh:mm:ss A");
let futureMoment = startMoment.subtract(1, "days");
will work.
I get dynamic date from my request,
Instant duration = request.getStartDate(); // my input is 2020-03-01T00:00:01Z
for (int i = 0; i < 12; i++) {
duration = duration.with(TemporalAdjusters.firstDayOfNextMonth ());
}
Basically i wanted to get first day of every month for 12 months, i tried with above code but getting exception,
java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: DayOfMonth
My output could be,
2020-04-01T00:00:01Z
2020-05-01T00:00:01Z
2020-06-01T00:00:01Z
:
:
2021-03-01T00:00:01Z
anything am missing here? tried with using plus()
duration.plus(30, ChronoUnit.DAYS);
I tried to convert instant to locateDate, then it prints fine, but for my required format i need to convert to Instant from localdate. I'm not seeing 1st day of next month.
2019-03-31T22:00:00Z
2019-04-30T22:00:00Z
2019-05-31T22:00:00Z
2019-06-30T22:00:00Z
2019-07-31T22:00:00Z
2019-08-31T22:00:00Z
2019-09-30T22:00:00Z
2019-10-31T23:00:00Z
2019-11-30T23:00:00Z
2019-12-31T23:00:00Z
any suggestion are appreciated. Thanks!
Much less fiddly than my namesake's answer: provided you convert to an OffsetDateTime up-front, there is no problem using the nice human-readable adjuster that you were trying to use initially.
OffsetDateTime duration = Instant.now().atOffset(ZoneOffset.UTC);
for (int i = 0; i < 12; i++) {
duration = duration.with(TemporalAdjusters.firstDayOfNextMonth());
}
The issue is that an Instant is an unambiguous point in time, irrespective of any specific timezone or geographic location. There is no specific date associated with an instant; what is Monday the 1st in one location may be Sunday the 31st in another, but it's still the same instant. That is why when trying to set a first day of the month, you get an exception.
If you convert to an OffsetDateTime, you are applying an offset (in this case UTC, so an offset of zero). This converts your data to a format in which a date is unambiguous.
Instant startInstant = request.getStartDate();
LocalDate start = startInstant.atZone(ZoneOffset.UTC).toLocalDate().withDayOfMonth(1);
for (int i = 0; i < 12; i++) {
System.out.println(start.plusMonths(i).atStartOfDay().toInstant(ZoneOffset.UTC));
}
I am trying to calculate the number of days between two dates using moment js.
function (value) {
var expiration= moment(value).format('DDMMYYYY');
var today = moment().format('DDMMYYYY');
var dayToExpiration = moment(expiration- today).format('D[days] ,H[hours]');
console.log(today + " : " + expiration
console.log(dayToExpiration);
The result is:
11102018 : 28102020 //--> 11.10.2018 : 28.10.2018
1 days ,6 hours //why only one day??
Because your dayToExpiration variable should be a moment.Duration object, not a string.
The difference between two datetimes is a duration, not a datetime.
Short answer:
As John Madhavan-Reese stated in his answer, you have to use moment Duration to represent the diffecence between two moments in time.
Issue in the code sample:
In your code you are creating a moment object from the difference between expiration and today. This value is interpreded by moment as the number of milliseconds since the Unix Epoch (see moment(Number)), so you are creating a moment object for a random day around the 1st January 1970 (see the output of moment(expiration- today).format() ). The D token in format() stands for Day of Month, so it gives an "incorrect" output.
My suggested solution:
You can calculate difference using momentjs' diff() then you can create a duration using moment.duration(Number).
Finally you can get your desired output using moment-duration-format plug-in (by John Madhavan-Reese :D)
Here a live sample:
function getDiff(value) {
var expiration= moment(value); // Parse input as momement object
var today = moment(); // get now value (includes current time)
// Calculate diff, create a duration and format it
var dayToExpiration = moment.duration(Math.abs(today.diff(expiration))).format('D[days], H[hours]');
console.log(today.format('DDMMYYYY') + " : " + expiration.format('DDMMYYYY'));
console.log(dayToExpiration);
}
getDiff('2018-10-28');
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/2.2.2/moment-duration-format.min.js"></script>
I am getting errors. this one works for me:
moment.duration(expiration.diff(today))._milliseconds / (1000*60*60*24));
I want to display the local time from an ISO 8601 string using momentjs.
There is a discrepancy of minutes when I convert an ISO string using different date formats. If I use 'MM/DD/YYYY HH:mm', the minutes is correctly displayed. If I use 'ddd, MMM Do HH:MMa', 11 minutes is added (in my case).
My sample js (babel) code:
let today = moment('11/09/2016 00:00', 'MM/DD/YYYY HH:mm').toISOString();
//today = 2016-11-09T08:00:00.000Z
let formatted = moment(today, moment.ISO_8601).format('MM/DD/YYYY HH:mm');
//formatted = 11/09/2016 00:00
let formatted2 = moment(today, moment.ISO_8601).format('ddd, MMM Do HH:MMa');
//formatted2 = Wed, Nov 9th 00:11am
I would prefer using the second format. Can someone explain why there is a discrepancy?
Please see this fiddle: https://jsfiddle.net/anudhagat/8fgtjbc7/3/
I caught my silly mistake. I have capitalized the minutes in the second format, using MM makes it display months instead of minutes.
i'm trying to calculate the difference between two UTC Datetime Strings with angular-momentjs like shown below:
var start = "1970-01-01T11:03:00.000Z";
var end = "1970-01-01T11:15:00.000Z";
var duration = $moment.utc($moment(end).diff($moment(start))).format("hh:mm");
when i execute the code above, the duration should be 00:12 but actually it is 12:12. I don't understand why and how to fix it.
You are actually creating a moment.js object for 1970-01-01T00:12:00.000Z, then getting the time as hour and minutes. The token "hh" is for 12 hour time, so you're seeing "12" for 12am. If you want to see 00:12, use the token "HH" which gives 24 hour time: 00:12.