Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sortable ("s") Format Specifier in C#
The Sortable ("s") format specifier in C# represents a standardized date and time format that produces ISO 8601-compliant strings. This format is particularly useful for sorting dates chronologically as strings and for data interchange between different systems.
The "s" format specifier is defined by the DateTimeFormatInfo.SortableDateTimePattern property and follows a consistent pattern regardless of the current culture settings.
Syntax
The sortable format specifier uses the following syntax −
DateTime.ToString("s")
This corresponds to the custom format pattern −
yyyy'-'MM'-'dd'T'HH':'mm':'ss
Format Pattern Breakdown
Basic Example
using System;
class Demo {
static void Main() {
DateTime date = new DateTime(2018, 9, 5, 2, 12, 40);
Console.WriteLine("Sortable format: " + date.ToString("s"));
// Multiple dates to show sorting capability
DateTime[] dates = {
new DateTime(2018, 12, 25, 15, 30, 45),
new DateTime(2018, 1, 15, 8, 22, 10),
new DateTime(2019, 3, 10, 12, 5, 30)
};
Console.WriteLine("\nDates in sortable format:");
foreach (DateTime d in dates) {
Console.WriteLine(d.ToString("s"));
}
}
}
The output of the above code is −
Sortable format: 2018-09-05T02:12:40 Dates in sortable format: 2018-12-25T15:30:45 2018-01-15T08:22:10 2019-03-10T12:05:30
String Sorting Demonstration
using System;
class SortableDemo {
static void Main() {
DateTime[] dates = {
new DateTime(2023, 6, 15, 14, 30, 0),
new DateTime(2022, 12, 1, 9, 45, 30),
new DateTime(2023, 6, 15, 9, 15, 45),
new DateTime(2024, 1, 10, 16, 20, 10)
};
// Convert to sortable strings
string[] sortableStrings = new string[dates.Length];
for (int i = 0; i < dates.Length; i++) {
sortableStrings[i] = dates[i].ToString("s");
}
Console.WriteLine("Before sorting:");
foreach (string s in sortableStrings) {
Console.WriteLine(s);
}
// Sort as strings - this works correctly with sortable format
Array.Sort(sortableStrings);
Console.WriteLine("\nAfter string sorting:");
foreach (string s in sortableStrings) {
Console.WriteLine(s);
}
}
}
The output of the above code is −
Before sorting: 2023-06-15T14:30:00 2022-12-01T09:45:30 2023-06-15T09:15:45 2024-01-10T16:20:10 After string sorting: 2022-12-01T09:45:30 2023-06-15T09:15:45 2023-06-15T14:30:00 2024-01-10T16:20:10
Parsing Sortable Format
using System;
using System.Globalization;
class ParseDemo {
static void Main() {
string sortableString = "2023-08-20T13:45:30";
// Parse back to DateTime
DateTime parsed = DateTime.ParseExact(sortableString, "s", CultureInfo.InvariantCulture);
Console.WriteLine("Original string: " + sortableString);
Console.WriteLine("Parsed DateTime: " + parsed);
Console.WriteLine("Back to sortable: " + parsed.ToString("s"));
// Verify round-trip
Console.WriteLine("Round-trip successful: " + (sortableString == parsed.ToString("s")));
}
}
The output of the above code is −
Original string: 2023-08-20T13:45:30 Parsed DateTime: 8/20/2023 1:45:30 PM Back to sortable: 2023-08-20T13:45:30 Round-trip successful: True
Common Use Cases
-
Database storage where dates need to be stored as sortable strings
-
File naming with timestamps for chronological ordering
-
Data interchange between systems requiring culture-independent formats
-
Logging systems where timestamp strings must sort correctly
Conclusion
The Sortable ("s") format specifier in C# produces culture-independent, ISO 8601-compliant date strings in the format yyyy-MM-ddTHH:mm:ss. This format is ideal for scenarios requiring chronological string sorting and cross-system date interchange without time zone information.
