Summary

Demonstrates how to convert a DateTime object to string representation using different format strings and cultures/locales. The format strings are provided in DataVoila's input editor.

Code (C#)

// Reset the output editor
Output.BodyRows.Clear();

// Get current date and time
DateTime now = DateTime.Now;

// Initialize the list of cultures to use for formatting the date
List<System.Globalization.CultureInfo> cultures = new List<System.Globalization.CultureInfo>();
cultures.Add(System.Threading.Thread.CurrentThread.CurrentCulture);
cultures.Add(new System.Globalization.CultureInfo("fr-FR"));
cultures.Add(new System.Globalization.CultureInfo("en-US"));

// Initialize header row
for(int i = 0; i < cultures.Count; i++)
{
    if (i + 1 >= Output.Columns.Count)
        Output.Columns.Add();
    // Add culture name to header
    Output.HeaderRow[i + 1] = cultures[i].DisplayName;
}

// Format date using the given format strings
foreach(string formatStr in Input.Lines)
{
    List<string> cellValues = new List<string>();
    cellValues.Add(formatStr);  // first output column contains the format string
    
    foreach(System.Globalization.CultureInfo culture in cultures)
    {
        cellValues.Add(now.ToString(formatStr, culture));
    }

    Output.BodyRows.Add(cellValues.ToArray());
} 
Input Type: Plain Text, Output Type: CSV

Download Project File

File: Format_date_time_strings.dvp (2.53 KB)

To open this file, DataVoila must be installed on your computer. If this is not yet the case, please click here to download the free demo version.