let myDate = new Date();
myDate.toLocaleString;
因此,如果我要控制台日志,则myDate
的值将为:
Wed Oct 16 2019 15:57:22 GMT+0300 (Israel Daylight Time)
如果我希望值只有57:22怎么办? (小时的分钟和秒)。 我该怎么办?
答案 0 :(得分:1)
一起去
const getMMSS = (str) => str.match(/:\d{2}:\d{2}/)[0].slice(1);
// tests
const myDate = new Date(2019,09,16,23,59,59,999);
let dateStr = myDate.toLocaleString();
console.log(getMMSS(dateStr))
dateStr = "Wed Oct 16 2019 15:57:22 GMT+0300 (Israel Daylight Time)"
console.log(getMMSS(dateStr))
或者只是
const pad = (num) => ("0"+num).slice(-2);
const myDate = new Date();
console.log(`${pad(myDate.getMinutes())}:${pad(myDate.getSeconds())}`)
答案 1 :(得分:0)
您可以使用import cats.free.Trampoline
import cats.free.Trampoline.{defer, done}
import cats.instances.function._
def pascalTriangle(c: Int, r: Int): Int = {
def hlp(c: Int, r: Int): Trampoline[Int] =
if (c == 0 || (c == r)) done(1)
else for {
x <- defer(hlp(c - 1, r - 1))
y <- defer(hlp(c, r - 1))
} yield (x + y)
hlp(c, r).run
}
自己的方法执行此操作:
Date
要使最终结果更具可读性,您可以检查分钟和秒是否小于10,如果是,则将0附加到开头:
let date = new Date();
let m = date.getMinutes();
let s = date.getSeconds();
console.log(m + ':' + s);
答案 2 :(得分:0)
您可以使用基本的javascript Date方法:
var myDate = new Date();
var formatted =`${myDate.getMinutes()}:${myDate.getSeconds()}`;
console.log(formatted);
或使用moment.js库
var myDate = new Date();
var formatted = moment(myDate).format("mm:ss");
console.log(formatted);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>