How to display datetime in the format with seconds? #1218
-
Help WantedI want the user can only choose date type calendar, and the displaying in the input is in the format like 2019-12-12 08:02:34 (the time user clicked the calendar)
ProblemLet's say i clicked the calendar in today at 08:02:34. the calendar should display 2019/12/12 08:02:34, but it shows 2019/12/12 8:2:0 |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
I didn't think the calendar had an option to pick seconds - but you can output it with seconds as you like. Looking above, it seems all you really need to do is check if the minute or second is less than 10 and pre-pend a zero to that, ie.
|
Beta Was this translation helpful? Give feedback.
-
@jamessampford thanks a lot. {
...
formatInput: false,
formatter: {
date: function(date, settings) {
if (!date) {
return '';
}
let year = date.getFullYear();
let month = date.getMonth() + 1;
month = month < 10 ? `0${month}` : month;
let day = date.getDate();
day = day < 10 ? `0${day}` : day;
let hour = date.getHours();
hour = hour < 10 ? `0${hour}` : hour;
let minute = date.getMinutes();
minute = minute < 10 ? `0${minute}` : minute;
let second = new Date().getSeconds();
second = second < 10 ? `0${second}` : second;
let s = `${year}/${month}/${day} ${hour}:${minute}:${second}`;
return s;
}
}
}; When i leave the calendar, it change the seconds number. So i add the formatInput: false.then it works fine, but i suggest that the calendar should provide an option to pick seconds and show the pre-zero. Anyway, thanks for your reply. |
Beta Was this translation helpful? Give feedback.
-
Hi, if you don't mind having one more lib in your project, you can probably use moment.js for this, e.g. let date = new Date()
moment(date).format('L LTS') Bonus: it handles the localization of the date and time too 🙂 |
Beta Was this translation helpful? Give feedback.
-
@vrialland Thanks for reply! |
Beta Was this translation helpful? Give feedback.
-
I think formatting date is not a direct issue of Fomantic UI but a JS-issue. |
Beta Was this translation helpful? Give feedback.
-
token string dateformat now supported by #2324 which also allows to show seconds via |
Beta Was this translation helpful? Give feedback.
I didn't think the calendar had an option to pick seconds - but you can output it with seconds as you like. Looking above, it seems all you really need to do is check if the minute or second is less than 10 and pre-pend a zero to that, ie.
let minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();