I want to convert the time from the standard 12-hour reading to the 24-hour one in simpleWeather. When I try to do it, it returns the time in the Unix epoch format.
$(function sunset(){
var sunset=
$.simpleWeather({
location:'Dalol,Afar',
woeid:'',
units:'f',
success:function(weather){
var Sunset= moment(weather.sunset, "HH:mm:ss");
html='Sunset: '+Sunset;
$('#sunset').html(html);
}
});
});
I had contacted the developer on how to do convert it using moment.js, but he didn't say much more than that. How can the code be corrected to display the desired format?
You need to first parse and then format the weather.sunset with the right formatting:
var sunset = moment(weather.sunset, ["h:mm A"]).format("HH:mm");
simpleWeather.js returns the sunset as "5:31 pm" so the corresponding moment format to parse this date is h:mm A, where the A captures the pm/am. To format it in the 24-hour notation, format("HH:mm") will do the job.
Related
I found lots of info on converting a duration object into various formats but it's harder to find info on converting a moment object into a duration in seconds.
This answer offers the following solution: myVar = moment.duration(myVar).asSeconds()
However it doesn't work in my case, myVar is in MM:SS format not HH:MM:SS format so I'm getting an aberrant result. Any idea how to adapt it to my situation?
EDIT: here's some code
this.totalTimeSimulation = moment(lastActionEndTime, 'mm:ss').add(additionalTimeDuration, 'seconds').format('mm:ss')
this.totalTimeSimulationInSeconds = moment.duration(this.totalTimeSimulation).asSeconds()
console.log(this.totalTimeSimulation)
console.log(this.totalTimeSimulationInSeconds)
In console I see:
04:00
14400
Should be:
04:00
240
Because 4 minutes equals 240 seconds, not 14400 seconds. Moment.js thinks I'm giving it a duration in HH:MM:SS format when actually I'm giving it in MM:SS format.
Moment threats input like 04:00 as HH:MM
The format is an hour, minute, second string separated by colons like 23:59:59. The number of days can be prefixed with a dot separator like so 7.23:59:59. Partial seconds are supported as well 23:59:59.999.
moment.duration('23:59:59');
moment.duration('23:59:59.999');
moment.duration('7.23:59:59.999');
moment.duration('23:59'); // added in 2.3.0
You can prefix your input with 00: or use moment.duration(Object) constructor using minutes and seconds keys:
const totalTimeSimulation = '04:00'
const totalTimeSimulationInSeconds = moment.duration('00:' + totalTimeSimulation).asSeconds()
console.log(totalTimeSimulation)
console.log(totalTimeSimulationInSeconds)
const parts = totalTimeSimulation.split(':')
const seconds = moment.duration({
minutes: parts[0],
seconds: parts[1]}).asSeconds()
console.log(seconds)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
I have a saved time that is in the format of this: moment().format('llll').
I want to convert that time to this format: YYYYMMDD?
I tried this:
let Time = moment().format('llll')
moment(Time, 'YYYYMMMDD')
That results in moment.invalid(/* 2019年7月4日星期四 16:03 */)
Anyone an idea on how to do it?
moment().format('llll') gives you a formatted string.
To create a moment object with a formatted string, you should also provide the string format.
let Time = moment().format('llll')
moment(Time, 'llll')
After that, you can easily format moment object as string
moment(Time, 'llll').format('YYYYMMMDD')
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.
How do I format a date as iso 8601 using moment.js but without the dashes and colons and setting the time to 0 e.g. if I have a date like this:
2016-10-08T09:00:00Z
How do I format as :
20161008T000000Z
Doing moment(date).toISOString() gives 2016-10-08T09:00:00.000Z which is not what I want.
You can simply parse your input into a moment object and use startOf to set time to 00:00:00. Then you can use format method to get a string in your custom format.
Here there is a working example using a string input, you can use the same code also if your input is a javascript Date object.
// Input date as string
var s = '2016-10-08T09:00:00Z';
// Reset time part
// var m = moment(s).startOf('day'); // no UTC
var m = moment.utc(s).startOf('day'); // UTC mode
// Format using custom format
console.log(m.format('YYYYMMDD[T]HHmmss[Z]'));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
My task is to use a datepicker to pick a date in the prescribed format, eg(MM-DD-YYYY) and pass it to the server as ISO formatted.
While it test the output the ISO formatted date is one day behind.
For example
If i select
07-13-2015
My Output ISO format is
ISO format is :2015-07-12T18:30:00.000Z
Here you can see date is 13 but the output date is 12
I am from India. I tried with zone and utcOffset, ended up with no results. How do i set it right
Here is the JSFIDDLE
js code
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(moment(selectedDate).toISOString());
});
I do have a hidden field which value will be updated on change and that will be processed in the server. No issues on that.
$('#datetimepicker1').on("dp.change",function(e){
var selectedDate = $('#datetimepicker1').find("input").val();
selectedDate = moment(selectedDate,"MM-DD-YYYY");
$(".temp").text(selectedDate.toISOString());
});
Your selectedDate is already a moment object so you do not need to feed it back into another moment.
Example:
var test = '07-13-2015'
var mtest = moment(test,"MM-DD-YYYY")
mtest.toISOString()
"2015-07-13T06:00:00.000Z"
Your could try converting the date format to UTC at once.
selectedDate = moment(selectedDate).utc('MM-DD-YYYY')
According to http://dygraphs.com/date-formats.html, if you pass a string like '07-13-2015', it means Midnight of 13th July 2015. Now, if you use toISOString function, it will convert it to UTC by default. To not convert it to UTC, just pass a parameter true in the toISOString function. (Moment.js docs)
For example:
var date = '07-13-2015';
date = moment(date,'MM-DD-YYY');
console.log(date.toISOString(true));
This way, moment will not convert the date to UTC.