What Is the Correct Date.Format for Mmm Dd, Yyyy Hh:Mm:Ss A? and How Convert to Dd-Mm-Yyyy Hh:Ii

What is the correct date.format for MMM dd, yyyy hh:mm:ss a? and how convert to dd-mm-yyyy HH:ii

A DateFormatter that converts between dates and their textual representations.

You can find more Locale identifier here.

  • first, convert your date to local time zone. using a Locale class
  • after you can covert the date in specific formate.

And try this code.

let dateStr = "Wed, 26 Jul 2017 18:10:02 +0530"
if let date = Date(fromString: dateStr, format: "MMM dd, yyyy hh:mm:ss a") {
debugPrint(date.toString(format: "dd-MM-yyyy HH:ii"))
}

Date Extension is ...

extension Date {

// Initializes Date from string and format
public init?(fromString string: String, format: String, identifier: String = Locale.current.identifier) {
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.locale = Locale(identifier: identifier)
if let date = formatter.date(from: string) {
self = date
} else {
return nil
}
}

// Converts Date to String, with format
public func toString(format: String, identifier: String = Locale.current.identifier) -> String {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: identifier)
formatter.dateFormat = format
return formatter.string(from: self)
}
}

String Extension is ...

extension String {
// Converts String to formated date string, with inputFormat and outputFormat
public func toDate(form inputFormat: String, to outputFormat: String, identifier: String = Locale.current.identifier) -> String? {
return Date(fromString: self, format: inputFormat, identifier: identifier)?.toString(format: outputFormat, identifier: identifier)
}

// Converts String to Date, with format
func toDate(format: String, identifier: String = Locale.current.identifier) -> Date? {
return Date(fromString: self, format: format, identifier: identifier)
}
}

format date from MMM dd, yyyy HH:mm:ss a to MM.dd

Three possible explanations:

  1. your default locale is incompatible with the input date - e.g. it can't understand Sep as a month name
  2. there's something wrong with the input string, or
  3. t is the wrong type (e.g. java.sql.Date instead of java.util.Date, or some other type altogether), or is not declared.

You should include details of the exception in your question to figure out which it is, but here's a working example using basically your own code, with the addition of a specific Locale.

SimpleDateFormat ft = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a", Locale.US);
java.util.Date t=ft.parse("Sep 16, 2015 10:34:23 AM");
ft.applyPattern("MM.dd");
System.out.println(ft.format(t));

output:

09.16

Converting string to datetime MMM DD YYYY HH:MMAM to YYYY-MM-DD HH:MM:SS

The Time::Piece module is convenient for this sort of thing

The strptime class method takes a date-time string and a format specification that it uses to parse the string to create a new object. Meanwhile the strftime method will return a date-time string according to another format specification

use strict;
use warnings 'all';

use Time::Piece ();

while ( <DATA> ) {
chomp;
my $new_dt = Time::Piece->strptime($_, '%b %d %Y %H:%M%p')->strftime('%Y-%m-%d %H:%M:%S');
printf "%s --> %s\n", $_, $new_dt;
}

__DATA__
Feb 2 2016 12:00AM
Feb 15 2015 05:00PM

output

Feb 2 2016 12:00AM --> 2016-02-02 00:00:00
Feb 15 2015 05:00PM --> 2015-02-15 17:00:00

converting dd/MM/yyyy hh:mm:ss to dd-MMM-yyyy hh:mm:ss in c# giving error : string is not recognized as valid datetime

If you know the format in which the date strings appear in the CSV file, you should be able to parse them using ParseExact.

Be sure to specify the exact format. In the ParseExact example you commented, you're supplying "dd-MMM-yyyy hh:mm:ss" as a format, but that would never work on dates that do not include a timestamp.

Assuming the two possible formats you've mentioned, this should work:

DateTime.ParseExact(
dateString,
new string[] { "dd/MM/yyyy", "dd-MMM-yyyy" },
CultureInfo.InvariantCulture,
DateTimeStyles.None);

PS: I prefer using TryParseExact by the way, to prevent an exception from being raised. That would look something like this:

    if (!DateTime.TryParseExact(
dateString,
new string[] { "dd/MM/yyyy", "dd-MMM-yyyy" },
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out DateTime parsedDateTime))
{
// parsing has failed, you could now
// - throw an exception
// - assign a default value to the date time field
// - ...
}

// parsedDateTime can be used here

Bigquery - Format element syntax for dd mmm yyyy hh:mm

For BigQuery Standard SQL

SELECT DATE(PARSE_DATETIME('%d %b %Y %H:%M',Created)) AS Created

Date conversion in MM/DD/YY h:mm:ss a format in java

As per the JavaDoc, D stands for Day in year. You will need to replace DD with dd (day in month).

Y stands for Week year, not year.

In short, this: "DD-MMM-YYYY" needs to be this: "dd-MMM-yyyy".

Pandas - Converting date column from dd/mm/yy hh:mm:ss to yyyy-mm-dd hh:mm:ss

If you know you will have a consistent format in your column, you can pass this to to_datetime:

df['sale_date'] = pd.to_datetime(df['sale_date'], format='%d/%m/%y %H:%M:%S')

If your formats aren't necessarily consistent but do have day before month in each case, it may be enough to use dayfirst=True though this is difficult to say without seeing the data:

df['sale_date'] = pd.to_datetime(df['sale_date'], dayfirst=True)


Related Topics



Leave a reply



Submit