Convert String Date to String date different format
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("2013-02-21");
System.out.println(format2.format(date));
How to convert a date string to different format
I assume I have import datetime
before running each of the lines of code below
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
prints "01/25/13"
.
If you can't live with the leading zero, try this:
dt = datetime.datetime.strptime("2013-1-25", '%Y-%m-%d')
print '{0}/{1}/{2:02}'.format(dt.month, dt.day, dt.year % 100)
This prints "1/25/13"
.
EDIT: This may not work on every platform:
datetime.datetime.strptime("2013-1-25", '%Y-%m-%d').strftime('%m/%d/%y')
Formatting a String date into a different format in Java
Use this code to format your `2017-05-23T06:25:50'
String strDate = "2017-05-23T06:25:50";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(strDate);
SimpleDateFormat sdfnewformat = new SimpleDateFormat("MMM dd yyyy");
String finalDateString = sdfnewformat.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
The converted finalDateString
set to your textView
Convert string of date format to string of another date format
try this
public string ConvertStringDateFormat(string date, string convertToDateFormat)
{
return Convert.ToString(Convert.ToDateTime(date),convertToDateFormat);
}
How do I convert the date from one format to another date object in another format without using any deprecated classes?
Use SimpleDateFormat#format
:
DateFormat originalFormat = new SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH);
DateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse("August 21, 2012");
String formattedDate = targetFormat.format(date); // 20120821
Also note that parse
takes a String
, not a Date
object, which is already parsed.
swift - Convert a String to a Date and then to a String in a different format
Updated for Swift 3.0
You need two date formatters - one to make sense of your input string and convert to a NSDate, and a different formatter to create the output string
let myDateString = "2016-01-01 04:31:32.0"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.A"
let myDate = dateFormatter.date(from: myDateString)!
dateFormatter.dateFormat = "MMM dd, YYYY"
let somedateString = dateFormatter.string(from: myDate)
I have updated this answer to change the date formatter from YYYY to yyyy. In most cases, there will be no difference between the two, but YYYY may treat the first few days of the year as being part of the last complete week of the previous year.
The Apple developer guides explain it like this.
A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
How to convert a javascript date string to another format?
You can try
var date = "26-08-2016";
var newdate = date.split("-").reverse().join("-");
Input : 26-08-2016
Output : 2016-08-26
Convert Date String to another Date string with different format
What you are doing is fine.
Probably you can improve it by using DateTime.TryParseExact
and on successful parsing, format the DateTime
object in other format.
string dateString = "20130916";
DateTime parsedDateTime;
string formattedDate;
if(DateTime.TryParseExact(dateString, "yyyyMMdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out parsedDateTime))
{
formattedDate = parsedDateTime.ToString("MM/dd/yyyy");
}
else
{
Console.WriteLine("Parsing failed");
}
Related Topics
Rails: Model Instance Method or Helper Method
Loading Class Descendants in Rails Development
How to Use a Named Scope in My Model Against an Array of Items
What's a Rails Plugin, or Ruby Gem, to Automatically Fix English Grammar
Readline Not Working in Rvm - Ubuntu 11.10
Get Gem Vendor Files in Asset Pipeline Path
How to Delete a Range of Values from an Array
Rails Calculate Date Range in Months
Call Before Methods in Model on Ruby
Ruby Convert Idn Domain from Punycode to Unicode
In Ruby How to Automatically Populate Instance Variables Somehow in the Initialize Method
How to Use Variables in a Yaml File
Recovering from a Broken Tcp Socket in Ruby When in Gets()
Installing Libyaml for Ruby on a MAC Osx (Lion)