JS format time (support applet, compatible with iOS)

Source: Internet
Author: User

 const REGEX =  /(\d{4})-(\d{2})-(\d{2})T(\d {2}) : (\d{2}) : (\d{2}) /
export const formattime = (val, format)+ ={
if (val) {
/**
* @instructions If it is not a timestamp format and contains the character '-' then '-' replace '-' with '/' && delete the decimal point and subsequent digits
* @reason '-' replace '-' with '/' && delete the decimal point and the following number because the Safari browser only supports '/' separated time formats
*/
if (val.toString(). indexOf ('-') > 0) {
val = val. Replace (/t/g, "). Replace (/\.[ \d]{3}z/, "). Replace (/(-)/g, '/'); //Replace '-' with '/'
val = val. Slice (0, Val. ) indexOf (".")); //delete the decimal point and subsequent digits
}
Let date = new date(val);
Date. sethours (date. getHours () + 8);
const [Whole, yy, MM, DD, hh, MM, ss] = date.toisostring (). Match (REGEX);
const Year = new Date (). getFullYear ();
const Month = new Date (). GetMonth () + 1;
Const Dates = new Date (). GetDate ();
if (format) {
return format
. Replace (' yyyy ', yy)
. Replace (' yy ', Yy.slice (2))
. Replace (' mm ', mm)
. Replace (' DD ', DD)
. Replace (' hh ', hh)
. Replace (' mm ', mm)
. Replace (' SS ', ss)
} else {
return [yy, MM, Dd].join('-') + ' + [hh, MM, SS]. Join (': ');
}
} else {
return '--';
}
}

JS format time (support applet, compatible with iOS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.