Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of Date of Safari browser in JS

Shulou Source: shulou.com Published: 2022-06-03 20:07:14 11月12日 Update

Editor to share with you what is the use of the Date of the Safari browser in JS, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

When dealing with Date in js, it is found that Safari and other browsers do not support the same way.

1. For example: 2017-01-01 12:00:00

In other browsers, there is no problem with using strings in this format for new Date operations, but Invalid Date is prompted in Safari

Date string in'- 'format is not supported in Safari. Replace it with' / 'and change it to 12:00:00 on 2017-01-01. It can be used normally.

two。 For example: 2017/01/01T11:00:00

The date string in this format is also not supported in Safari. You need to replace the'T'in the string with a space.

Into 11:00:00 format on 2017-01-01

The following is the function I wrote to format the date string. Although it is not perfect, I haven't found any problems yet.

/ * formatted time string * / * datetimeString: a time string that conforms to the time format * / / * returns the formatted time string: YYYY-MM-DD hh:mm:ss*/formatDatetime = function (datetimeString) {var datetime; if (isNaN (Date.parse (datetimeString) datetime = new Date (Date.parse (datetimeString.replace (/-/ g,'/'). Replace (/ Tamp g,'')) Else datetime = new Date (Date.parse (datetimeString)); var year = datetime.getFullYear (); / * year * / var month = datetime.getMonth () + 1 beat * month * / var date = datetime.getDate (); / * Day * / var hour = datetime.getHours (); / * hour * / var minute = datetime.getMinutes (); / * minutes * / var second = datetime.getSeconds () / * the format of January, day, hour, minute and second * / month = month < 10?'0' + month: month; date = date < 10?'0' + date: date; hour = hour < 10?'0' + hour: hour; minute = minute < 10?'0' + minute: minute; second = second < 10?'0' + second: second Return year +'-'+ month +'-'+ date +'+ hour +':'+ minute +':'+ second;}; these are all the contents of the article "what is the use of Date in Safari browsers in JS". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Format character string browser browse time date article support content question consistency not much function most way is in more knowledge spaces Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Docker vpn Shulou Tech Info