What is ISO Date?
Weekday, ordinal Date, and calendar day are the three fundamental categories.
A calendar date is represented by a 4-digit year number, a 2-digit month number, a 2-digit day number. For instance, August 2, 1953, could be expressed as follows:
19530802 or 1953/08/02
A four-digit year number is followed by a three-digit number representing the number of days in the year when writing an ordinal date. For instance, August 2, 1953, could be written as follows:
1953214 or 1953-214
A weekday is represented by a 4-digit year number, a W, a 2-digit week number, a 1-digit weekday number (1 = Monday, 2 = Tuesday,…, 7 = Sunday), and a 4-digit week number. For instance, August 2, 1953, could be written as follows:
1953W317 or 1953-W31-7
Note that you must always write all the digits. Therefore, year 47 must be written as 0047.
Use the split() method
A string can be divided into an array of substrings using the
split() method of a string object, which returns the new array.
- separator: Specifies the character or regular expression to divide the string or separator. The complete string is returned if it is omitted (An array with only one item).
- limit: An integer indicating the number of splits; items that are included in the array after the division limit.
We can use this method to split the Date ISO string into the string we need to find and the string containing milliseconds to get the value at the discretion of the problem.
You can see that if we create a
new Date() to get the current time, the second time will be separated from the millisecond by a “
.” dot symbol. So we use the slice method to split the string at the dot symbol position and get the string with index equal to one, and then print it to the console.
Use the slice() method
slice() method has the function of extracting part of a string. The method will return a new string, the quoted portion of the original string.
Use the start and end parameters to specify the part to be extracted from the original string. Note that the first position of the string is 0, followed by one, .etc
- start: The starting position to extract.
- end: The extraction end position. The extracted content will not include the character at the end position. If no end parameter is provided, the method will extract from the start position to the end of the string.
console.log(new Date().toISOString().slice(0, -5));
In this approach, we will rely on the
Date() pattern. We can see that milliseconds in ISO will have four values, so we will use the slice method to cut the part we don’t want to get, which is the millisecond and print the string we need. Find the screen. The pulse return result is the same as above. I hope this is helpful to you.
split() method or
slice() method. Let’s try two ways. Good luck for you!
Maybe you are interested:
Name of the university: PTIT