DateAndTime.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Word;
using GrapeCity.Documents.Word.Fields;
using System.Globalization;

namespace DsWordWeb.Demos
{
    // This sample shows how to add simple date and time fields to a Word document.
    public class DateAndTime
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();

            var date = new DateFieldOptions(doc);
            doc.Body.AddParagraph("DATE field with default formatting: ").AddComplexField(date);

            var time = new TimeFieldOptions(doc);
            doc.Body.AddParagraph("TIME field with default formatting: ").AddComplexField(time);

            doc.Body.AddParagraph(
                "The following table demonstrates some custom date/time formats. " +
                "The left column shows the date/time format, the right column contains the calculated field value.");

            // Add and setup a table to contain the samples:
            var t = doc.Body.Tables.Add(0, 0);
            t.Style = doc.Styles.Add("Table style 1", StyleType.Table);
            foreach (var border in t.Style.Table.Borders)
            {
                border.LineStyle = LineStyle.Single;
                border.LineWidth = 0.5f;
                border.Color.RGB = Color.Black;
            }
            // Sample DATE formats:
            string[] sampleDateFormats = [
                "M/d/yyyy",
                "dddd, MMMM dd, yyyy",
                "MMMM d, yyyy",
                "M/d/yy",
                "yyyy-MM-dd",
                "d-MMM-yy",
                "M.d.yyyy",
                "MMM. d, yy",
                "d MMMM yyyy",
                "MMMM yy",
                "MMM-yy",
                "M/d/yyyy h:mm am/pm",
                "M/d/yyyy h:mm:ss am/pm",
                "h:mm am/pm",
                "h:mm:ss am/pm",
                "HH:mm",
                "'Today is 'MMMM d, yyyy",
            ];
            // Add the sample format strings and corresponding DATE fields:
            foreach (string fmt in sampleDateFormats)
            {
                var r = t.Rows.Add(fmt);
                var dfo = new DateFieldOptions(doc) { DateTimeFormat = fmt };
                r.Cells.Add().GetRange().Paragraphs.First.AddComplexField(dfo);
            }

            // Update all fields using a specific culture:
            doc.UpdateFields(new GrapeCity.Documents.Word.Layout.WordLayoutSettings() { FontCollection = Util.FontCollection, Culture = CultureInfo.GetCultureInfo("en-US") });
            // Done:
            return doc;
        }
    }
}