How to Convert Date (Without Knowing the Kind) to String

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 Strings. Only Strings can represent one particular format of a date.



Related Topics



Leave a reply



Submit