I have this date getting from an API Call in a JSON Format: CreatedOn : 2018-08-27T05:11:29.000Z
When I open the form, I am setting the datetime picker to that field.
<ion-datetime displayFormat="MMM DD YYYY h:mm A" pickerFormat="MMM DD YYYY h:mm A"
[(ngModel)]="Task.CreatedOn"></ion-datetime>
Getting here Aug 27 2018 5:11 AM which is wrong
I am also showing the same date with angular.
<h6>{{Task.CreatedOn | date : 'MMMM d,yyyy At hh:mma' }}</h6>
Getting here August 27,2018 At 08:11AM Which is right.
I think there is a timezone issue with the datetime picker when I am setting it.
How can I solve this issue?
You can use Angular formatToLocal pipe which will internally convert using moment.
<ion-datetime
displayFormat="MMM DD YYYY h:mm A"
pickerFormat="MMM DD YYYY h:mm A"
[ngModel]="Task.CreatedOn | formatToLocal"
(ngModelChange)="Task.CreatedOn=$event">
</ion-datetime>
PIPE:
import {Pipe, PipeTransform} from '#angular/core'
import moment from 'moment';
#Pipe({ name: 'formatToLocal'})
export class FormatToLocal implements PipeTransform{
transform(val) {
if(val){
return moment(val).format();
}
}
}
Related
How to format flutter DateTime
as 10th Oct 2020
tried
String date = DateFormat("dd, mm, y").format (date.utc());//10 October 2020
you can use the Jiffy package from here ==> https://pub.dev/packages/jiffy.
this is an example of formatting DateTime:
import 'package:jiffy/jiffy.dart';
void main() {
DateTime currentTime = DateTime.now();
String result = Jiffy(currentTime).format('MMM do yy');
print(result);
}
Try out this package, Jiffy
Just simply add the do date pattern. See below
Jiffy([2020, 10, 10]).format("do MMM yyyy"); //10th Oct 2020
You can also add your DateTime object
Jiffy(DateTime(2020, 10, 10)).format("do MMM yyyy"); //10th Oct 2020
I'm getting a expiry time when i call the log in api in for format of "Thu, 30 Dec 2021 10:24:34 GMT" i want to get the current time and compare it.
By using DateFormat class in intl package,
you can parse from string date to DateTime.
And by using 'isBefore' or 'isAfter' method, you can compare two DateTime.
import 'package:intl/intl.dart';
void main() {
String strDate = "Thu, 30 Dec 2021 10:24:34 GMT";
DateFormat format = DateFormat("EEE, dd MMM yyyy hh:mm:ss");
DateTime parsedDate = format.parse(strDate);
print('parsedDate: $parsedDate');
DateTime now = DateTime.now();
print('now: $now');
print('parsedDate is before now: ${parsedDate.isBefore(now)}');
print('parsedDate is after now: ${parsedDate.isAfter(now)}');
}
If I understand your question correctly, this can help.
DateTime now = DateTime.now();
DateTime then = DateTime.parse(expirlyDateFromApi.toDate().toString());
var difference = now.difference(expirlyDate);
I am trying to millisecondsSinceEpoch 1619451874928 to dd-MM-YYYY format in flutter. Is there any method that does that?
How to convert eg. 1619451874928 to 27 March 2021
import 'package:intl/intl.dart';
void main() {
final dateTime = DateTime.fromMillisecondsSinceEpoch(1619451874928);
final dateTimeString = DateFormat("dd-MM-yyyy").format(dateTime);
print(dateTimeString);
final dateTimeStringLong = DateFormat("dd MMMM yyyy").format(dateTime);
print(dateTimeStringLong);
}
dateTimeString will be in format you requested, i.e. "dd-MM-yyyy". Hovever in order to get date in format of "27 March 2021", you should use following pattern
"dd MMMM yyyy".
Please note that you should also include and import package 'package:intl/intl.dart'
You can use this:
DateFormat("dd MM yyyy").format((DateTime.fromMillisecondsSinceEpoch(1619451874928)));
In my project I have a standard DateField format as "dd MMM yyyy". I used setDateFormat("dd MMM yyyy") to se this format. But now users want to enter "MM/dd/yyyy", "MM-dd-yyyy" and "MM dd yyyy" formats too, with the displayable date MUST still be "dd MMM yyyy".
Right now when I enter "31/01/2016" in the DateField with setDateFormat("dd MMM yyyy") I am getting "date format not recognized" error.
My question is how can I make a datefield accept multiple date format inputs(not using the calendar picker).
Any help is much appreciated. Thanks for reading the post!!!
You can override a method handleUnparsableDateString:
public class MyDateField extends DateField {
#Override
protected Date handleUnparsableDateString(String dateString) throws Converter.ConversionException {
return super.handleUnparsableDateString(dateString);
}
}
That method is called when DateField is not able to parse the input. You can parse the input in the method and return a correct Date instance.
I have to parse dates in formats:
HH:mm
dd MMM
dd MMM yyyy
I've managed to handle the last two of them:
val dateParsers = Array(
DateTimeFormat.forPattern("dd MMM").getParser,
DateTimeFormat.forPattern("dd MMM yyyy").getParser,
ISODateTimeFormat.timeParser().getParser
)
val formatter = new DateTimeFormatterBuilder().append(null, dateParsers).toFormatter.withZoneUTC
DateTime.parse(updatedString, formatter.withDefaultYear(currentYear).withLocale(ruLocale))
Everything is ok with dd MMM and dd MMM yyyy, but when I'm trying parse time like 05:40 I'm getting 01-01-1970 date instead of today's date. What is the simplest method to set default date as today's date in parser?
Joda-Time-Formatter only supports withDefaultYear(), not things like withDefaultDate(). Instead you can do this:
DateTimeFormatter timeParser = DateTimeFormat.forPattern("HH:mm");
LocalTime time = timeParser.parseLocalTime("05:40");
DateTimeZone tz = DateTimeZone.getDefault(); // Or DateTimeZone.UTC or DateTimeZone.forID( "Europe/Paris" )
LocalDate today = LocalDate.now(tz);
DateTime moment = today.toLocalDateTime(time).toDateTime(tz);
System.out.println(moment);
// output in my local timezone: 2014-08-20T05:40:00.000+02:00
Note: I have written the solution in Java, because I am not a scala-guy.
Date and Time are completely different, without subclassing DateTimeFormatter and implementing your special "time at todays' date"-algorithm you wont get very far. Either subclass or maybe inject your current date into the string if it matches some regular expression
I'd use withDate()
private static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern("HH:mm");
#Test
public void test() {
DateTime dateTime = FORMATTER.parseDateTime("05:40");
DateTime now = new DateTime();
dateTime = dateTime.withDate(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth());
System.out.println(dateTime.toDate());
}