How to convert String to Date without knowing the format?
You cant!
If you have the date 2010-08-05
then it can be either 5th August 2010, or 8th May 2010 - you need to know the date format (or at least prioritise one format over the over) to tell them apart.
How to convert date (without knowing the kind) to string
Example using NSDataDetector
(Swift 2.0):
let textDate = "dan's march 31, 1975 12:34 pm";
do {
let dd = try NSDataDetector(types: NSTextCheckingType.Date.rawValue)
if let tcr :NSTextCheckingResult = dd.firstMatchInString(textDate, options:[], range:NSMakeRange(0, textDate.utf16.count)),
let date = tcr.date {
print("date: \(date)")
}
}
catch {
print("Data Detector error!")
}
Output:
date: 1975-03-31 16:34:00 +0000
Convert string to date object without using the format
You need to know some sort of formatting information. If you have a few ideas, you can just iterate through them.
private static String[] formats = new String[] {/* Your list of possible formats */};
public static Date parse(String date) throws ParseException {
for (String format : formats) {
DateFormat df = new SimpleDateFormat(format);
try {
return df.parse(date);
} catch (ParseException e) {}
}
throw new ParseException(
"This date does not conform to any known format", 0);
}
Of course, this is only if the same date won't fulfill multiple formats (e.g., 01/01/01 as mentioned in comments)
Python: How can I convert string to datetime without knowing the format?
super short answer:
from dateutil import parser
parser.parse("8:36pm")
>>>datetime.datetime(2015, 6, 26, 20, 36)
parser.parse("18:36")
>>>datetime.datetime(2015, 6, 26, 18, 36)
Dateutil should be available for your python installation; no need for something large like pandas
If you want to extract the time from the datetime
object:
t = parser.parse("18:36").time()
which will give you a time
object (if that's of more help to you).
Or you can extract individual fields:
dt = parser.parse("18:36")
hours = dt.hour
minute = dt.minute
How to convert date in string with or without time into DATE
You can modify the pars based on the presence of time,
if(dateOrDataTime.contains(":")){
//Use parser with date-time. & fetch with equal
}else{
//Use parser for date only. & fetch using date(date_time)
}
Convert Java Date from one format to another without converting it into string
Date
represents a single point in time. That's it, nothing more, nothing less. It does not contain any information about time zones.
Wed Jun 06 12:38:15 BST 2018
is the same Date
(instant) as Wed Jun 06 11:38:15 GMT 2018
, just in a different time zone. It's like the words "humans" and "homo sapians". They refer to the same species, they are just kind of in a different "format".
So you don't need to change the date in any way. You just need to format it differently. This is why formatters return String
s. Only String
s can represent one particular format of a date.
Related Topics
Audiokit - How to Get Real Time Floatchanneldata from Microphone
What Are the First Two Columns in Scnmatrix4
Custom Sequence for Swift Dictionary
How to Get the Correct Current Time in iOS
How to Auto Call an @Ibaction Function
Swiftui Tabbedview Only Shows First Tab's Content
How to Calculate a Random Cgpoint Which Does Not Touch a Uiview
How to Access My Swift Classes from My UI Tests
Converting an Existing Project into Customizable Framework
How to Call Rating View from JSON and Show the Rating in My Table View
Swift, Google Map Fit Bound for All the Markers
Fbsdkcorekit.Framework/Fbsdkcorekit: No Matching Architecture in Universal Wrapper
How to Eliminate the Margin on the Left Side of a Uitableview, Without Creating a Gap on the Right
Timer Label Not Updated After Switching Views (Swift)
How to Pass in a Void Block to Objc_Setassociatedobject in Swift