I'm using momentjs to work with dates in my project for when a user enters a date in M/D/YYYY format to revert to MM/DD/YYYY format (e.g. 2/5/2017 to 02/05/2017). I am also converting any invalid dates or dates greater than today to be reset back to today's date.
element.on("blur", function() {
var currentDate = moment().format('MM/DD/YYYY');
var formattedInput;
if (ctrl.$modelValue !== undefined && ctrl.$modelValue !== "") {
if(moment(ctrl.$modelValue, "MM/DD/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue);
formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate);
ctrl.$render();
}
} else if (moment(ctrl.$modelValue, "M/D/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue);
formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate);
ctrl.$render();
} else {
ctrl.$setViewValue(formattedInput.format('MM/DD/YYYY'));
ctrl.$render();
}
} else {
ctrl.$setViewValue(currentDate);
ctrl.$render();
}
}
});
As far as I can tell, this is all working fine with the code I have above. But regardless of working functionality, I am receiving the deprecation warning for non-ISO dates. My thoughts are the use of MM/DD/YYYY format, however this is unchangeable due to business requirements. Is there a way to remedy this issue in a non-cumbersome way?
The problem is with formattedInput = moment(ctrl.$modelValue) here you are using moment parsing without format with non-ISO dates. To remove the Deprecation warning, just use moment(ctrl.$modelValue, "MM/DD/YYYY") and moment(ctrl.$modelValue, "M/D/YYYY") as you have done in the if condition.
Your complete code will be the following:
element.on("blur", function() {
var currentDate = moment();
var formattedInput;
if (ctrl.$modelValue !== undefined && ctrl.$modelValue !== "") {
if(moment(ctrl.$modelValue, "MM/DD/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue, "MM/DD/YYYY", true);
// This line returns a string, but does not assign to value, so it's superfluous
//formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
ctrl.$render();
}
} else if (moment(ctrl.$modelValue, "M/D/YYYY", true).isValid()) {
formattedInput = moment(ctrl.$modelValue, "M/D/YYYY", true);
// see previous comment
//formattedInput.format('MM/DD/YYYY');
if (formattedInput.isAfter(currentDate)) {
ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
ctrl.$render();
} else {
ctrl.$setViewValue(formattedInput.format('MM/DD/YYYY'));
ctrl.$render();
}
} else {
ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
ctrl.$render();
}
}
});
Be sure to fully understand the difference between moment parsing (build a moment object from a string) and moment format (display a string representation of a moment object).
Related
ion datetime isDateEnabled is not working. i already added isDateEnabled in HTML, but not working.
here is my code -
== HTML ==
<ion-datetime [isDateEnabled]="isDateEnabled('2022-06-26T00:00:00.000Z')" [firstDayOfWeek]="1" presentation="date" ></ion-datetime>
=== TS file ===
isDateEnabled(dateIsoString: string) {
const date = new Date(dateIsoString);
if (getYear(date) === 2022) {
return false;
}
return true;
}
I hope you have a good day. You need to pass a function in the isDateEnabled property
.html
<ion-datetime [isDateEnabled]="isDateEnabled"></ion-datetime>
.ts
// arrow function allow access global scope
isDateEnabled = (dateIsoString: string): boolean => {
return true;
};
I am creating a shared agenda that has to be filled by different people. Everytime they modify a certain cell, the date of change (day) is reported in another cell on the same row. The code below is currently working, but there are 3 minor problems:
The local time is not correct (I am living in italy)
I would like to see the time as well (hours:minutes)
If the modified cell is cleared (like if the text is deleted), also the date of change should disappear.
I find this type of code difficult to debug and would appreciate any help.
Thank you in advance,
function onEdit(event) {
var cell = event.source.getActiveCell();
var sheet = cell.getSheet();
var headers= sheet.getDataRange().getValues()[0];
var statusCol = headers.indexOf('Status') + 1 ;
var ss = event.source.getActiveSheet();
if (event.range.columnStart != 2) {
} else {
ss.getRange(event.range.rowStart, 5)
.setValue(getDate(new Date()));
}
var tt = event.source.getActiveSheet();
if (event.range.columnStart != 34) {
return;
} else {
tt.getRange(event.range.rowStart, 37)
.setValue(getDate(new Date()));
}
}
function getDate(d) {
if (d) {
return d.getDate() + "/" + (d.getMonth()+1) + "/" + d.getYear();
}
}
Add TimeZone, Hours, Minutes and deletes Datetime if cell contents is deleted.
It adds hours and minutes in 24 hour format with a timezone correction and if e.value is null then it deletes the contents of the datetime cell.
function onEdit(e) {
var ss=e.source;
var rg=e.range;
var sh=rg.getSheet();
if(rg.columnStart!=2 && rg.columnStart!=34){return;}
if(rg.columnStart==2){
if(e.value) {
sh.getRange(rg.rowStart, 5).setValue(Utilities.formatDate(new Date(new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate(),new Date().getHours(),new Date().getMinutes()),Session.getScriptTimeZone(),"d/MMM/yyyy HH:mm"));
}else{
sh.getRange(rg.rowStart, 5).setValue('');
}
}
if (rg.columnStart==34) {
if(e.value) {
sh.getRange(rg.rowStart, 37).setValue(Utilities.formatDate(new Date(new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate(),new Date().getHours(),new Date().getMinutes()),Session.getScriptTimeZone(),"d/MMM/yyyy HH:mm"));
}else{
sh.getRange(rg.rowStart, 37).setValue('');
}
}
}
Spreadsheet Edit Event Object
Utilities.formatDate()
I'm following a tutorial from here
For some reason when I try to create a new user with a date it won't accept it unless the month is January between dates ranging from 1-12ish.
I'm pretty sure it's because of the ValidationMessageFor(in the User.cs) method which forces me to enter a date which month must be January and I don't know where to alter it.
jquery.validate
jquery.validate.unobtrusive
Add code into script
$.validator.addMethod('date', function (value, element) {
if (this.optional(element)) {
return true;
}
var valid = true;
try {
$.datepicker.parseDate('dd/mm/yy', value);
}
catch (err) {
valid = false;
}
return valid;
});
$('#dt1').datepicker({ dateFormat: 'dd/mm/yy' });
I write some code to check that now date is tomorrow :
re_dat = SDKClass.Selct_Date_now(); // return today date from database.
DateTime date_now = DateTime.Parse(re_dat).Date;
if (date_now == DateTime.Now.AddDays(1).Date)
{
response.write("tomorrow");
}
but condition doesn't fire.
thanks a lot.
If your question is about difference with some dynamic input date and today date as reference date, then you can use dates difference like this:
var dateDiff = (DateTime.Today - inputDate.Date).TotalDays;
Here is my simple version of date formatting:
public static string GetDateString(DateTime inputDate)
{
var dateDiff = (DateTime.Today - inputDate.Date).TotalDays;
if (dateDiff == 0)
{
return "TODAY";
}
else if(dateDiff == 1)
{
return "YESTERDAY";
}
else if(dateDiff == -1)
{
return "TOMORROW";
}
else
{
return inputDate.ToShortDateString();
}
}
you shouldn't retrive the today date from database.
i think best solutin for this:
if (DateTime.Today == DateTime.Today.AddDays(1))
{
Response.Write("Tomorrow");
}
else { //Write whatever you want }
I am writing Xamarin.iOS app using C#.
I am trying to convert EST timezone time to local timezone time using following code, but it is never converted.
TimeZoneInfo eastZone = TimeZoneInfo.FindSystemTimeZoneById ("EST");
DateTime convertedDate = TimeZoneInfo.ConvertTime (estDate, eastZone, TimeZoneInfo.Local);
convertedDate is always same as estDate.
Of course, I am not in EST timezone.
I checked ConvertTime() definition, and found it assumes first parameter is local timezone.
public static DateTime ConvertTime (DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo destinationTimeZone)
{
if (sourceTimeZone == null) {
throw new ArgumentNullException ("sourceTimeZone");
}
if (destinationTimeZone == null) {
throw new ArgumentNullException ("destinationTimeZone");
}
if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone != TimeZoneInfo.Local) {
throw new ArgumentException ("Kind property of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local");
}
if (dateTime.Kind == DateTimeKind.Utc && sourceTimeZone != TimeZoneInfo.Utc) {
throw new ArgumentException ("Kind property of dateTime is Utc but the sourceTimeZone does not equal TimeZoneInfo.Utc");
}
if (sourceTimeZone.IsInvalidTime (dateTime)) {
throw new ArgumentException ("dateTime parameter is an invalid time");
}
if (dateTime.Kind == DateTimeKind.Local && sourceTimeZone == TimeZoneInfo.Local && destinationTimeZone == TimeZoneInfo.Local) {
return dateTime;
}
DateTime dateTime2 = TimeZoneInfo.ConvertTimeToUtc (dateTime);
if (destinationTimeZone != TimeZoneInfo.Utc) {
dateTime2 = TimeZoneInfo.ConvertTimeFromUtc (dateTime2, destinationTimeZone);
if (dateTime.Kind == DateTimeKind.Unspecified) {
return DateTime.SpecifyKind (dateTime2, DateTimeKind.Unspecified);
}
}
return dateTime2;
}
.Net System.dll has wrong code?
I am also seeing problems with TimeZoneInfo.ConvertTime except on Android.
I filed bug 25942 for this on Xamarin's site: https://bugzilla.xamarin.com/show_bug.cgi?id=25942