ACTUAL ISSUE:
am using momentjs and below code to validate date format
date = moment(dateVal, fmt);
if format is YYYY-MM-DD, and if dateVal is 2017-04, then date value is not coming as null. moment function is sending 2017-04-01 as the date back instead of "invalid date" or null. how do i change the code for the moment to show invalid date.
i tried this
date = moment(dateVal, fmt, true);
but for some reason when i use this, isValid is always coming as false, even when i send valid date and format. for eg, 1/4/2017 with format MM/DD/YYYY is giving error with date = moment(dateVal, fmt, true)
here is my code
var CustomDateValidate = function (dateVal, fmt) {
var date = null;
if (dateVal !== null && dateVal !== undefined) {
fmt = defaultFor(fmt, getHiddenData().dateFormatJavascriptMoment); // here for en the fmt will be "MM/DD/YYYY" and for fr the fmt will be "YYYY-MM-DD"
date = moment(dateVal, fmt, true);
if (!(date.isValid())) {
date = null;
}
//else
if (date.year().toString().length < 4) {
date = null;
} else if (date.toString().toLowerCase() === 'invalid date') {
date = null;
} else if (date._i.toString().length > 10) {
date = null;
}
}
return date;
plz help.
Related
I am trying to find a date in a sheet and it does work, but when I return the startdate it does not function correctly.
function startdayfinder(NomClient)
{
const passageclientsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Client Sheet");
const numlastrowIS = passageclientsheet.getLastRow()-1;
const dataIS = passageclientsheet.getRange(2,1,numlastrowIS,3).getValues();
var start;
dataIS.forEach(function(row,i)
{
if (row[0]==NomClient)
{
if(row[2]=="oui")
{
start = Utilities.formatDate(row[1], "GMT+1", "dd/MM/yyyy");
}
else
{
Logger.log(start);
return start;//(Utilities.formatDate(start, "GMT+1", "dd/MM/yyyy"));
}
}
});
}
And this is how I call this function:
var startday = startdayfinder(row[0]) ;
Unfortunately, I don't know where it goes wrong because the function returns null, but the logger shows that the date has been successfully written into the start variable.
Where did I mess up?
Try with getDisplayValues()
const dataIS = passageclientsheet.getRange(2,1,numlastrowIS,3).getDisplayValues()
assuming that dates are also formatted as dd/MM/yyyy in your sheet
I'am trying to convert a datetime string from one format to another. i tried to use intl package. But i don't know how to convert this string to another format.
The datetime string i'am getting from api is 14/12/2021 03:34:03 PM. I want to show it like this in my app (only time) 03:34 pm (Also want to make PM Small letter).
Try below code hope its helpful to you. I have tried it without using any third party library
String yourDate = '14/12/2021 03:34:03 PM';
DateFormat formateDate = DateFormat('dd/MM/yyyy hh:mm:ss a');
DateTime inputDate = formateDate.parse(yourDate);
String resultDate = DateFormat('hh:mm a').format(inputDate);
print(resultDate.toLowerCase());
Your Widget:
Text(
'Time : ${resultDate.toLowerCase()}',
style: TextStyle(
fontSize: 15,
),
),
Your Result Screen->
If you don't mind doing it the regexp way
import 'package:intl/intl.dart';
void main() {
var s = "14/12/2021 12:34:03 PM";
print(formatDateTimeString(s));
}
String formatDateTimeString(String s) {
RegExp regexp = RegExp("(AM|PM)");
// finds either AM or PM in string and converts it to lowercase
RegExpMatch match = regexp.firstMatch(s)!;
String end = match.group(0)!.toLowerCase();
// change datetime format to HH:mm as desired
DateTime dt = DateFormat("d/MM/yyyy HH:mm:ss").parse(s);
DateFormat newFormat = DateFormat("HH:mm");
String formattedS = newFormat.format(dt);
// append lowercase ampm
formattedS += " " + end;
return formattedS;
}
You can get it by using below,
dateFormat() {
String dateStart = '14/12/2021 03:34:03 PM';
DateFormat inputFormat = DateFormat("dd/MM/yyyy hh:mm:ss a");
DateTime input = inputFormat.parse(dateStart);
String dateOutput = DateFormat("hh:mm a").format(input);
return dateOutput.toLowerCase();
}
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);
How to format DateTime in Flutter
please check out, it's for all formated datetime , you just pass your current and required date format
import 'package:intl/intl.dart';
void main() {
var data = "14/12/2021 03:34:03 PM";
print(getFormattedDateFromFormattedString(
value: data,
currentFormat: "yyyy/MM/dd HH:mm:ss a",
desiredFormat: "hh:mm a").toLowerCase()); //03:34 pm
}
String getFormattedDateFromFormattedString(
{required value,
required String currentFormat,
required String desiredFormat,
isUtc = false}) {
String formattedDate = "";
if (value != null || value.isNotEmpty) {
try {
DateTime dateTime =
DateFormat(currentFormat).parse(value, isUtc).toLocal();
formattedDate = DateFormat(desiredFormat).format(dateTime);
} catch (e) {
print("$e");
}
}
// print("Formatted date time: $formattedDate");
return formattedDate;
}
I have a string like this String dt = '04-09-2021 - 15:00'
i want to convert it into local time zone
and my code is:
if(dt != 'Not Available'){
return DateFormat('dd-MM-yyyy - HH:mm')
.format(DateTime.parse(dt).toLocal())
.toString();
}else{
return dt;
}
But i get the error: Invalid date format 04-09-2021 - 15:00
You should use parse, not format, the format functions receive a date, but you don't have a date, you have an string, use:
DateFormat('dd-MM-yyyy - HH:mm').parse(dt);
Here I wrote a dateformatter and convert it to local time. As well as you can format like as you wish.
// call getFormattedDateFromFormattedString function in Text
Center(
child: Text(getFormattedDateFromFormattedString(
currentFormat: "dd-MM-yyyy - HH:mm",
desiredFormat: "dd MMM yyyy HH:mm a",
value: "04-09-2000 - 15:00")),
)
// date fomatter function
String getFormattedDateFromFormattedString(
{#required String currentFormat,
#required String desiredFormat,
String value}) {
String formattedDate = "";
if (value != null || value.isNotEmpty) {
try {
DateTime dateTime = DateFormat(currentFormat).parse(value, true).toLocal();
formattedDate = DateFormat(desiredFormat).format(dateTime);
} catch (e) {
print("$e");
}
}
// print("Formatted date time: $formattedDate");
return formattedDate.toString();
}
Output:
N.B: My time zone GMT+6.0
I am making a spreadsheet that requires finding out if a certain cell range has been edited and then add the date that it was edited on. I have the range of cells needed to be edited, and I also don't want the date to change if I sort the table from a different column.
I would think the general idea would look something like this, not actual code just an idea:
cell_range=(b2:d2)
if (cell_range)==editted:
date()
if (table_sort==True):
date_change==False
If anyone knows how to do this, that would be greatly appreciated.
you need a script for this. try for example:
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) {
var r = s.getActiveCell();
if( r.getColumn() == 2 ) {
var nextCell = r.offset(0, 8);
var newDate = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy");
nextCell.setValue(newDate);
}
if( r.getColumn() == 3 ) {
var nextCell = r.offset(0, 7);
var newDate = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy");
nextCell.setValue(newDate);
}
if( r.getColumn() == 4 ) {
var nextCell = r.offset(0, 6);
var newDate1 = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy");
nextCell.setValue(newDate1);
}}}
"Sheet1" = sheet name
r.getColumn() == 3 = column C / 3rd column
r.offset(0, 7) = offset timestamp 7 columns to the right on the same row eg column H
"GMT+8" = timezone
"MM/dd/yyyy hh:mm:ss" = date and time format
alternative (untested):
function onEdit(e) {
if ([2].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 7).setValue(new Date()); }
if ([3].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 6).setValue(new Date()); }
if ([4].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 5).setValue(new Date()); } }
I want to check whether end date is greater than or equal to start date, with jquery validate. It is validating end date greater than start date, but it is not permitting end date equal to start date,
here is my code -
jQuery.validator.addMethod("greaterThan",
function(value, element, params) {
if (!/Invalid|NaN/.test(new Date(value))) {
return new Date(value) > new Date($(params).val());
}
return isNaN(value) && isNaN($(params).val())
|| (Number(value) > Number($(params).val()));
},'Must be greater than {0}.');
jQuery("#collect_and_delivery").validate({
errorElement:'div',
rules: {
from:{
required:true
},
to:{
required:true
},
start_date:{
required:true
},
end_date:{
required:true,
greaterThan: "#start_date"
}
},
messages: {
from:"Please enter collect address",
to:"Please enter delivery address",
start_date:"Please enter shipping collect date",
end_date:
{
required:"Please enter shipping delivery date",
greaterThan:"Delivery date and Collect date should be proper"
}
}
});
You should use is greater than or equal to(>=) in your custom method greaterThan.
Change this line:
return new Date(value) > new Date($(params).val());
To:
return new Date(value) >= new Date($(params).val());
Enjoy...
If you want to use simple javascript to compare ethe date. Here is one; simple and easier.
function compareDate() {
var str = document.getElementById("start_date").value;
var end = document.getElementById("end_date").value;
var year = str.substring(0,4);
var month = str.substring(5,7);
var date = str.substring(8,10);
var endYear = end.substring(0,4);
var endMonth = end.substring(5,7);
var endDate = end.substring(8,10);
var startDate = new Date(year, month-1, date);
var endDate = new Date(endYear, endMonth-1, endDate);
if (startDate > endDate) {
alert('start date should be less than end date');
return false;
}
else { return true; }
}