convert datetime to date format dd/mm/yyyy
DateTime dt = DateTime.ParseExact(yourObject.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string s = dt.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
how to format String to Date with format dd-mm-yyyy in java
tl;dr
LocalDate
.parse( "2022-05-12" )
.format(
DateTimeFormatter.ofPattern( "dd-MM-uuuu" )
)
12-05-2022
java.time
Use modern java.time classes. Never use the terrible Date
, Calendar
, SimpleDateFormat
classes.
ISO 8601
Your input conforms to ISO 8601 standard format used by default in the java.time classes for parsing/generating text. So no need to specify a formatting pattern.
LocalDate
Parse your date-only input as a date-only object, a LocalDate
.
String input = "2022-05-12" ;
LocalDate ld = LocalDate.parse( input ) ;
To generate text in your desired format, specify a formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;
String output = ld.format( f ) ;
Rather than hardcode a particular pattern, I suggest learning to automatically localize using DateTimeFormatter.ofLocalizedDate
.
All this has been covered many many times already on Stack Overflow. Always search thoroughly before posting. Search to learn more.
How to convert dd/mm/yyyy string into JavaScript Date object?
MM/DD/YYYY format
If you have the MM/DD/YYYY
format which is default for JavaScript, you can simply pass your string to Date(string)
constructor. It will parse it for you.
var dateString = "10/23/2015"; // Oct 23
var dateObject = new Date(dateString);
document.body.innerHTML = dateObject.toString();
Converting dd/mm/yyyy formatted string to Datetime
You need to use DateTime.ParseExact
with format "dd/MM/yyyy"
DateTime dt=DateTime.ParseExact("24/01/2013", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Its safer if you use d/M/yyyy
for the format, since that will handle both single digit and double digits day/month. But that really depends if you are expecting single/double digit values.
Your date format day/Month/Year
might be an acceptable date format for some cultures. For example for Canadian Culture en-CA
DateTime.Parse
would work like:
DateTime dt = DateTime.Parse("24/01/2013", new CultureInfo("en-CA"));
Or
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
DateTime dt = DateTime.Parse("24/01/2013"); //uses the current Thread's culture
Both the above lines would work because the the string's format is acceptable for en-CA
culture. Since you are not supplying any culture to your DateTime.Parse
call, your current culture is used for parsing which doesn't support the date format. Read more about it at DateTime.Parse.
Another method for parsing is using DateTime.TryParseExact
DateTime dt;
if (DateTime.TryParseExact("24/01/2013",
"d/M/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt))
{
//valid date
}
else
{
//invalid date
}
The TryParse
group of methods in .Net framework doesn't throw exception on invalid values, instead they return a bool
value indicating success or failure in parsing.
Notice that I have used single d
and M
for day and month respectively. Single d
and M
works for both single/double digits day and month. So for the format d/M/yyyy
valid values could be:
- "24/01/2013"
- "24/1/2013"
- "4/12/2013" //4 December 2013
- "04/12/2013"
For further reading you should see: Custom Date and Time Format Strings
Format JavaScript date as yyyy-mm-dd
You can do:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate('Sun May 11,2014'));
DateTime Convert from MMM-yyyy to dd-MM-yyyy or yyyy-MM-dd
Use
DateTime.ParseExact(dt, "MMM-yyyy", CultureInfo.InvariantCulture)
This will give you first of the month anyway.
Change date format dd-MM-yyyy to yyyy-MM-dd in Java
Try this (see update below)
try {
String startDateString = "08-12-2017";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf2.format(sdf.parse(startDateString)));
} catch (ParseException e) {
e.printStackTrace();
}
Update - Java 8
String startDateString = "08-12-2017";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
System.out.println(LocalDate.parse(startDateString, formatter).format(formatter2));
How to convert a dd.MM.yyyy string to a Date object
If you absolutely have to have it in that format, you can rearrange it quick before using the date object as it expects a certain format..
Something like this could work.
if( typeof myVar === 'string') {
let dateArr = myVar.split('.');
let myDate = new Date(dateArr[1] + '-' + dateArr[0] + '-' + dateArr[2]);
if (myDate.getTime() < this.getMinDate().getTime()) /** compare two dates */
//omitted source code
}
Related Topics
Differencebetween Null and Empty
Getting List Ips from Cidr Notation in PHP
PHP MySQL - Insert New Record into Table with Auto-Increment on Primary Key
Should Lock_Ex on Both Read & Write Be Atomic
Laravel 5.3 Withcount() Nested Relation
In PHP, How to Get the First and Last Date of a Month
Send Checkbox Value in PHP Form
Set Product Sale Price Programmatically in Woocommerce 3
Uncompress a Gzip File from Curl, on PHP
Adding Multiple Items to Woocommerce Cart at Once
Magento - Load Only Configurable Products
PHP How to Remove Any Last Commas
How Many Days Until X-Y-Z Date
How to Use File_Get_Contents() to Compare Two Files
Mysql/SQL Retrieve First 40 Characters of a Text Field