Permalink
Browse files

Q.formatDate can now accept an iso date/time string or normal date st…

…ring
  • Loading branch information...
1 parent bc0c50e commit a3b950c278d1aa0f84664a75deafb6a57ff1cdde @volkanceylan committed Dec 11, 2016
@@ -18,6 +18,12 @@ public static string FormatDate(this JsDate date, string format = null)
return null;
}
+ [InlineCode("Q.formatDate({date}, {format})")]
+ public static string FormatDate(string date, string format = null)
+ {
+ return null;
+ }
+
[InlineCode("Q.formatISODateTimeUTC({date})")]
public static string FormatISODateTimeUTC(JsDate date)
{
@@ -51,5 +51,59 @@ public void FormatDateWorks()
Q.Culture.DateTimeFormat = backupDateTimeFormat;
}
}
+
+ [Test]
+ public void FormatDateWorksWithISOString()
+ {
+ var backupDec = Q.Culture.DateSeparator;
+ var backupDateFormat = Q.Culture.DateFormat;
+ var backupDateTimeFormat = Q.Culture.DateTimeFormat;
+ try
+ {
+ Q.Culture.DateSeparator = "/";
+ Q.Culture.DateFormat = "dd/MM/yyyy";
+ Q.Culture.DateTimeFormat = "dd/MM/yyyy HH:mm:ss";
+
+ Assert.AreEqual(Q.FormatDate("2029-01-02"), "02/01/2029", "'/': date only, empty format");
+ Assert.AreEqual(Q.FormatDate("2029-01-02T16:35:24"), "02/01/2029", "'/': date with time, empty format");
+ Assert.AreEqual(Q.FormatDate("2029-01-02T16:35:24", "g"), "02/01/2029 16:35", "'/': date with time, g format");
+
+ Q.Culture.DateSeparator = ".";
+ Assert.AreEqual(Q.FormatDate("2029-01-02"), "02.01.2029", "'.': date only, empty format");
+ Assert.AreEqual(Q.FormatDate("2029-01-02T16:35:24"), "02.01.2029", "'.': date with time, empty format");
+ Assert.AreEqual(Q.FormatDate("2029-01-02T16:35:24", "g"), "02.01.2029 16:35", "'.': date with time, g format");
+ }
+ finally
+ {
+ Q.Culture.DecimalSeparator = backupDec;
+ Q.Culture.DateFormat = backupDateFormat;
+ Q.Culture.DateTimeFormat = backupDateTimeFormat;
+ }
+ }
+
+ [Test]
+ public void FormatDateWorksWithDateString()
+ {
+ var backupDec = Q.Culture.DateSeparator;
+ var backupDateFormat = Q.Culture.DateFormat;
+ var backupDateTimeFormat = Q.Culture.DateTimeFormat;
+ try
+ {
+ Q.Culture.DateSeparator = "/";
+ Q.Culture.DateFormat = "dd/MM/yyyy";
+ Q.Culture.DateTimeFormat = "dd/MM/yyyy HH:mm:ss";
+
+ Assert.AreEqual(Q.FormatDate("2/1/2029"), "02/01/2029", "'/': date only, empty format");
+
+ Q.Culture.DateSeparator = ".";
+ Assert.AreEqual(Q.FormatDate("2/1/2029"), "02.01.2029", "'.': date only, empty format");
+ }
+ finally
+ {
+ Q.Culture.DecimalSeparator = backupDec;
+ Q.Culture.DateFormat = backupDateFormat;
+ Q.Culture.DateTimeFormat = backupDateTimeFormat;
+ }
+ }
}
}
@@ -47,12 +47,12 @@ public DateEditor(jQueryObject input)
if (!string.IsNullOrEmpty(MinValue) &&
String.Compare(value, MinValue) < 0)
return String.Format(Q.Text("Validation.MinDate"),
- Q.FormatDate(Q.ParseISODateTime(MinValue)));
+ Q.FormatDate(MinValue));
if (!string.IsNullOrEmpty(MaxValue) &&
String.Compare(value, MaxValue) >= 0)
return String.Format(Q.Text("Validation.MaxDate"),
- Q.FormatDate(Q.ParseISODateTime(MaxValue)));
+ Q.FormatDate(MaxValue));
return null;
});
@@ -230,7 +230,7 @@ public String Value
if (value != null && value.Length == 0)
return null;
- return Q.FormatDate(Q.Externals.ParseDate(value), "yyyy-MM-dd");
+ return Q.FormatDate(value, "yyyy-MM-dd");
}
set
{
@@ -239,7 +239,7 @@ public String Value
else if (value.ToLower() == "today" || value.ToLower() == "now")
this.element.Value(Q.FormatDate(JsDate.Today));
else
- this.element.Value(Q.FormatDate(Q.Externals.ParseISODateTime(value)));
+ this.element.Value(Q.FormatDate(value));
}
}
@@ -58,12 +58,12 @@ public DateTimeEditor(jQueryObject input, DateTimeEditorOptions opt)
if (!string.IsNullOrEmpty(MinValue) &&
String.Compare(value, MinValue) < 0)
return String.Format(Q.Text("Validation.MinDate"),
- Q.FormatDate(Q.ParseISODateTime(MinValue)));
+ Q.FormatDate(MinValue));
if (!string.IsNullOrEmpty(MaxValue) &&
String.Compare(value, MaxValue) >= 0)
return String.Format(Q.Text("Validation.MaxDate"),
- Q.FormatDate(Q.ParseISODateTime(MaxValue)));
+ Q.FormatDate(MaxValue));
return null;
});
@@ -125,7 +125,7 @@ public string Value
if (value != null && value.Length == 0)
return null;
- var datePart = Q.FormatDate(Q.Externals.ParseDate(value), "yyyy-MM-dd");
+ var datePart = Q.FormatDate(value, "yyyy-MM-dd");
var timePart = this.time.GetValue();
return datePart + "T" + timePart + ":00.000";
}
@@ -544,6 +544,46 @@
Q.Culture.dateTimeFormat = backupDateTimeFormat;
}
}));
+ test('FormatDateWorksWithISOString', ss.mkdel(this, function() {
+ var backupDec = Q.Culture.dateSeparator;
+ var backupDateFormat = Q.Culture.dateFormat;
+ var backupDateTimeFormat = Q.Culture.dateTimeFormat;
+ try {
+ Q.Culture.dateSeparator = '/';
+ Q.Culture.dateFormat = 'dd/MM/yyyy';
+ Q.Culture.dateTimeFormat = 'dd/MM/yyyy HH:mm:ss';
+ deepEqual(Q.formatDate('2029-01-02', null), '02/01/2029', "'/': date only, empty format");
+ deepEqual(Q.formatDate('2029-01-02T16:35:24', null), '02/01/2029', "'/': date with time, empty format");
+ deepEqual(Q.formatDate('2029-01-02T16:35:24', 'g'), '02/01/2029 16:35', "'/': date with time, g format");
+ Q.Culture.dateSeparator = '.';
+ deepEqual(Q.formatDate('2029-01-02', null), '02.01.2029', "'.': date only, empty format");
+ deepEqual(Q.formatDate('2029-01-02T16:35:24', null), '02.01.2029', "'.': date with time, empty format");
+ deepEqual(Q.formatDate('2029-01-02T16:35:24', 'g'), '02.01.2029 16:35', "'.': date with time, g format");
+ }
+ finally {
+ Q.Culture.decimalSeparator = backupDec;
+ Q.Culture.dateFormat = backupDateFormat;
+ Q.Culture.dateTimeFormat = backupDateTimeFormat;
+ }
+ }));
+ test('FormatDateWorksWithDateString', ss.mkdel(this, function() {
+ var backupDec = Q.Culture.dateSeparator;
+ var backupDateFormat = Q.Culture.dateFormat;
+ var backupDateTimeFormat = Q.Culture.dateTimeFormat;
+ try {
+ Q.Culture.dateSeparator = '/';
+ Q.Culture.dateFormat = 'dd/MM/yyyy';
+ Q.Culture.dateTimeFormat = 'dd/MM/yyyy HH:mm:ss';
+ deepEqual(Q.formatDate('2/1/2029', null), '02/01/2029', "'/': date only, empty format");
+ Q.Culture.dateSeparator = '.';
+ deepEqual(Q.formatDate('2/1/2029', null), '02.01.2029', "'.': date only, empty format");
+ }
+ finally {
+ Q.Culture.decimalSeparator = backupDec;
+ Q.Culture.dateFormat = backupDateFormat;
+ Q.Culture.dateTimeFormat = backupDateTimeFormat;
+ }
+ }));
}
});
ss.initClass($Serenity_Test_QDialogTests, $asm, {
@@ -4,11 +4,30 @@
namespace Q {
- export function formatDate(date: Date, format?: string) {
- if (!date) {
+ export function formatDate(d: Date | string, format?: string) {
+ if (!d) {
return '';
}
+ let date: Date;
+ if (typeof d == "string") {
+ if (d.length >= 10 && d.charAt(4) === '-' && d.charAt(7) === '-' &&
+ (d.length === 10 || (d.length > 10 && d.charAt(10) === 'T'))) {
+ date = Q.parseISODateTime(d);
+ }
+ else {
+ var z = Q.parseDate(d);
+ if (z === false)
+ return d;
+ date = z as Date;
+ }
+
+ if (!date)
+ return d;
+ }
+ else
+ date = d;
+
if (format == null || format == "d") {
format = Culture.dateFormat;
}
@@ -284,7 +284,7 @@ declare namespace Q {
function toId(id: any): any;
}
declare namespace Q {
- function formatDate(date: Date, format?: string): string;
+ function formatDate(d: Date | string, format?: string): string;
function formatDayHourAndMin(n: number): string;
function formatISODateTimeUTC(d: Date): string;
function parseISODateTime(s: string): Date;
@@ -501,10 +501,27 @@ var Q;
/// <reference path="Q.Number.ts" />
var Q;
(function (Q) {
- function formatDate(date, format) {
- if (!date) {
+ function formatDate(d, format) {
+ if (!d) {
return '';
}
+ var date;
+ if (typeof d == "string") {
+ if (d.length >= 10 && d.charAt(4) === '-' && d.charAt(7) === '-' &&
+ (d.length === 10 || (d.length > 10 && d.charAt(10) === 'T'))) {
+ date = Q.parseISODateTime(d);
+ }
+ else {
+ var z = Q.parseDate(d);
+ if (z === false)
+ return d;
+ date = z;
+ }
+ if (!date)
+ return d;
+ }
+ else
+ date = d;
if (format == null || format == "d") {
format = Q.Culture.dateFormat;
}
Oops, something went wrong.
Oops, something went wrong.

0 comments on commit a3b950c

Please sign in to comment.