Datetime format globalization change [duplicate] - date

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 2 years ago.
I want to convert different date format to current date format
Example :
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("tr-TR");
string dateString = "16.03.2020 10:50:00"; //(I record the value) (now format dd.MM.yyyy HH:mm:ss)
//Change Culture (en-US)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Datetime convertedDate = Convert.ToDatetime(dateString); //(now format dd/MM/yyyy HH:mm:ss)
error = String was not recognized as a valid DateTime
how can i solve ?

I solved it this way
string dateString = "16.03.2020 10:55:10"; //Çağdaş SANCARBARLAZ
DateTime lastCheckDate = Convert.ToDateTime(dateString,
System.Globalization.CultureInfo.GetCultureInfo("tr-TR").DateTimeFormat);
Result :
Thank you everyone

Related

how to calculate difference between two date and get year month and days in flutter

How can I change the Date format in dart
I get the from and to date difference value is int. How to change this integer to date format... and convert it to 0Days 0Months 7days;
I need this type of format
but I got this type of format
see the Vehicle Age:
vehicleAge(DateTime doPurchase, DateTime doRenewel) {
age = doPurchase.difference(doRenewel).abs().inDays.toInt();
return age;
}
That is my function...
Use this package time_machine and try this code
vehicleAge(DateTime doPurchase, DateTime doRenewel) {
LocalDate a = LocalDate.dateTime(doPurchase);
LocalDate b = LocalDate.dateTime(doRenewel);
Period diff = b.periodSince(a);
return "${diff.years} Years ${diff.months} Months ${diff.days} Days";
}
Try with this
vehicleAge(DateTime doPurchase, DateTime doRenewel) {
Duration parse = doPurchase.difference(doRenewel).abs();
return "${parse.inDays~/360} Years ${((parse.inDays%360)~/30)} Month ${(parse.inDays%360)%30} Days";
}
Correct Answer
I got a correct answer use jiffy package.
import 'package:jiffy/jiffy.dart';
then use this code
vehicleAge(DateTime doPurchase, DateTime doRenewel) {
var dt1 = Jiffy(doPurchase);
var dt2 = Jiffy(doRenewel);
int years = int.parse("${dt2.diff(dt1, Units.YEAR)}");
dt1.add(years: years);
int month = int.parse("${dt2.diff(dt1, Units.MONTH)}");
dt1.add(months: month);
var days = dt2.diff(dt1, Units.DAY);
return "$years Years $month Month $days Days";
}

How to convert String formatted date to timestamp in Flutter [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
Closed 1 year ago.
I have a string in this format.
var stringDate = '27-04-2021 19:45' //dd-MM-yyyy HH:mm
I want to convert this String date to Timestamp (millisecondsSinceEpoch)
import 'package:intl/intl.dart';
getCustomFormattedDateTime(String givenDateTime, String dateFormat) {
// dateFormat = 'MM/dd/yy';
final DateTime docDateTime = DateTime.parse(givenDateTime);
return DateFormat(dateFormat).format(docDateTime);
}
You can use this method
getCustomFormattedDateTime('2021-02-15T18:42:49.608466Z', 'MM/dd/yy');
Result:
02/15/21

How to convert a String of certain format to Date in Swift? [duplicate]

This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 2 years ago.
I have strings like this 2020-09-21T21:13:55.636Z... I want to convert them into Dates and here's what I do;
func convertToDate(str: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter.date(from: str)!
}
The above code crashes on the unwrapping. I'm guessing it's because of the format... Any idea what I'm doing wrong?
For that date string you should use ISO8601DateFormatter, like this:
var str = "2020-09-21T21:13:55.636Z"
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = formatter.date(from: str)
That will save you the trouble of trying to get a format string exactly right.

Dart parse full dateTime including am/pm

I have my date here 2020/07/07 09:47:54 from my API in which I'm trying to parse to 2020/07/07 09:47am. I have tried the following method using intl package but I'm getting an error
DateFormat format = DateFormat('yyyy/MM/dd hh:mm').add_jm();
DateTime newDate = format.parse(date)
Unhandled Exception: FormatException: Trying to read from 2020/07/07 09:47:54 at position 20
Anyone has an idea what seems wrong here?
I think you need to add this line of code:
String date = DateFormat.yMEd().add_jms().format(DateTime.now());
String dateWithT = date.substring(0, 8) + 'T' + date.substring(8);
DateTime dateTime = DateTime.parse(dateWithT);
Solution
I found a way in which i need to parse the string date into a DateTime format then reformat it again based on what I needed.
DateTime dateTime = DateFormat('yyyy/MM/dd h:m').parse(date);
final DateFormat formatter = DateFormat('yyyy-MM-dd h:m a');
final String formatted = formatter.format(dateTime);
You can try formatting it twice like this:
String date = '2020/07/07 09:47:54';
DateFormat myDateFormat = DateFormat('yyyy/MM/dd hh:mm');
DateTime newDate = myDateFormat.parse(date);
var myString = myDateFormat.format(newDate) + DateFormat('a').format(newDate);

Handling date format in swift [duplicate]

This question already has answers here:
How can I convert string date to NSDate?
(18 answers)
Closed 6 years ago.
The timestamp from the php rest service is coming out in string format:
2016-06-08 09:03:59
How to convert it to NSDate in my iOS code. Swift answer preferred. Thanks!
Here is one way:
// Initialize Date string
var dateStr = "2016-06-08 09:03:59"
// Set date format
var dateFmt = NSDateFormatter()
dateFmt.timeZone = NSTimeZone.defaultTimeZone()
dateFmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
// Get NSDate for the given string
var date = dateFmt.dateFromString(dateStr)
print(date!)
Date is now of type NSDate.