Saltar al contenido

ejemplo de código de flutter de fecha y hora

Ejemplo 1: flutter obtiene la fecha actual

import 'package:intl/intl.dart';

main() {
  static final DateTime now = DateTime.now();
  static final DateFormat formatter = DateFormat('yyyy-MM-dd');
  final String formatted = formatter.format(now);
  print(formatted); // something like 2013-04-20
}
// https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html

Ejemplo 2: formato de fecha y hora de flutter

import 'package:intl/intl.dart';

DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd – kk:mm').format(now);

Ejemplo 3: flutter datetimeoffset

// DateTime can only represent local time and UTC time.
// It supports timezone offset for parsing, but normalizes it to UTC

print(DateTime.parse('2018-09-07T17:29:12+02:00').isUtc);
// prints true.

//You can then only convert between local and UTC time using:
// toLocal() or toUtc()

Ejemplo 4: flutter date time

var now = new DateTime.now();
var moonLanding = DateTime.parse(now);

moonLanding.year; 
moonLanding.hour;  
moonLanding.month; 

moonLanding.year.toString() + '-' + moonLanding.month.toString() + '-' + moonLanding.day.toString()

############################ OR ###########################

import 'package:intl/intl.dart';

var dt = DateTime.now();
var newFormat = DateFormat("yy-MM-dd");
String updatedDt = newFormat.format(dt);
print(updatedDt); // 20-04-03

var newDt = DateFormat.yMMMEd().format(dt);
print(newDt);  // Fri, Apr 3, 2020

reference :
https://medium.com/flutter-community/working-with-dates-in-dart-e81c70911811

Ejemplo 5: fecha de flutter con zona horaria

/// converts [date] into the following format: `2020-09-16T11:55:01.802248+01:00`
static String formatISOTime(DateTime date) {
  var duration = date.timeZoneOffset;
  if (duration.isNegative)
  	return (date.toIso8601String() + "-${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
  else
  	return (date.toIso8601String() + "+${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *