public void SetMillisecondsWorks()
        {
            var dt = new JsDate(2000, 0, 1);

            dt.SetMilliseconds(435);
            Assert.AreEqual(dt.GetMilliseconds(), 435);
        }
        public void YMDHNSUConstructorWorks()
        {
            var dt = new JsDate(2011, 7, 12, 13, 42, 56, 345);

            Assert.AreEqual(dt.GetFullYear(), 2011);
            Assert.AreEqual(dt.GetMonth(), 7);
            Assert.AreEqual(dt.GetDate(), 12);
            Assert.AreEqual(dt.GetHours(), 13);
            Assert.AreEqual(dt.GetMinutes(), 42);
            Assert.AreEqual(dt.GetSeconds(), 56);
            Assert.AreEqual(dt.GetMilliseconds(), 345);
        }
        public void ToLocalWorks()
        {
            var utc = new JsDate(2011, 7, 12, 13, 42, 56, 345);
            var dt  = utc.ToLocalTime();

            Assert.AreEqual(dt.GetUtcFullYear(), utc.GetFullYear());
            Assert.AreEqual(dt.GetUtcMonth(), utc.GetMonth());
            Assert.AreEqual(dt.GetUtcDate(), utc.GetDate());
            Assert.AreEqual(dt.GetUtcHours(), utc.GetHours());
            Assert.AreEqual(dt.GetUtcMinutes(), utc.GetMinutes());
            Assert.AreEqual(dt.GetUtcSeconds(), utc.GetSeconds());
            Assert.AreEqual(dt.GetUtcMilliseconds(), utc.GetMilliseconds());
        }
        public void GetMillisecondsWorks()
        {
            var dt = new JsDate(2011, 7, 12, 13, 42, 56, 345);

            Assert.AreEqual(dt.GetMilliseconds(), 345);
        }
Beispiel #5
0
        /// <summary>
        /// Converts a date to string according to a specified format
        /// </summary>
        /// <param name="date">Date to format, can be null</param>
        /// <param name="format">Format specifier</param>
        /// <returns>Formatted date, or empty string if date is null</returns>
        public static string FormatDate(this JsDate date, string format = null)
        {
            if (date == null)
            {
                return("");
            }

            if (format == null)
            {
                format = Q.Culture.DateFormat;
            }

            Func <int, string> pad = i => i.ToString().PadLeft(2, '0');

            return(format.Replace(new Regex("dd?d?d?|MM?M?M?|yy?y?y?|hh?|HH?|mm?|ss?|tt?|fff|zz?z?|\\/", "g"),
                                  delegate(string fmt)
            {
                switch (fmt)
                {
                case "/":
                    return Q.Culture.DateSeparator;

                case "hh":
                    return pad(date.GetHours() < 13 ? date.GetHours() : (date.GetHours() - 12));

                case "h":
                    return (date.GetHours() < 13 ? date.GetHours() : (date.GetHours() - 12)).As <string>();

                case "HH":
                    return pad(date.GetHours());

                case "H":
                    return date.GetHours().As <string>();

                case "mm":
                    return pad(date.GetMinutes());

                case "m":
                    return date.GetMinutes().As <string>();

                case "ss":
                    return pad(date.GetSeconds());

                case "s":
                    return date.GetSeconds().As <string>();

                case "yyyy":
                    return date.GetFullYear().As <string>();

                case "yy":
                    return date.GetFullYear().ToString().Substring(2, 4);

                case "dddd":
                    return date.As <dynamic>().GetDayName();

                case "ddd":
                    return date.As <dynamic>().GetDayName(true);

                case "dd":
                    return pad(date.GetDate());

                case "d":
                    return date.GetDate().ToString();

                case "MM":
                    return pad((date.GetMonth() + 1));

                case "M":
                    return (date.GetMonth() + 1).As <string>();

                case "t":
                    return (date.GetHours() < 12 ? "A" : "P");

                case "tt":
                    return (date.GetHours() < 12 ? "AM" : "PM");

                case "fff":
                    return (date.GetMilliseconds()).ToString().PadLeft(3, '0');

                case "zzz":
                case "zz":
                case "z":
                    return "";

                default:
                    return fmt;
                }
            }));
        }