Skip to content

Date Formatting

image Source: adopted from here

Introduction

Date format varies from country to country and every programming language provides one way or another to format a date object into a string of different formats. Date format by country gives a good summary of different format used in different countries and the ISO 8601 date format.

Question

Convert a date string of format yyyy.mm.dd to yyyymmdd. Provide as many solutions as possible and compare the speed of all methods.

s:string 2020.06.29; / Convert it to "20200629"

Answer

Below are 10 different methods to answer this question. Method 2 is the most commonly used approach by most users, but it is one of the slowest. Below it records the time to run each method 1 million times. I repeated each timing three times and the median time spent is reported. Method 1 is the slowest and Method 10 is the fastest. The 10 methods show you how to use many different q keywords and operators.

// Method 1: 2402ms
\t:1000000 raze sublist[;s] each (4;5 2;-2)

// Method 2: 1966ms
\t:1000000 ssr[s;".";""]

// Method 3: 631ms
\t:1000000 (4#s),(2#-5#s),-2#s

// Method 4: 592ms
\t:1000000 "" sv "." vs s

// Method 5: 478ms
\t:1000000 raze "." vs s

// Method 6: 413ms
\t:1000000 s where s<>"."

// Method 7: 388ms
\t:1000000 s where not s="."

// Method 8: 383ms
\t:1000000 except[;"."] s

// Method 9: 297ms
\t:1000000 s except "."

// Method 10: 248ms
\t:1000000 s _/ 4 6