How to convert an ISO date to the date format yyyy-mm-dd?
Try this
date = new Date('2013-03-10T02:00:00Z');
date.getFullYear()+'-' + (date.getMonth()+1) + '-'+date.getDate();//prints expected format.
Update:-
As pointed out in comments, I am updating the answer to print leading zeros for date and month if needed.
date = new Date('2013-08-03T02:00:00Z');year = date.getFullYear();month = date.getMonth()+1;dt = date.getDate();
if (dt < 10) { dt = '0' + dt;}if (month < 10) { month = '0' + month;}
console.log(year+'-' + month + '-'+dt);
Convert date string from ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ ) to 'DD-MM-YYYY HH:mm`
Since your input string has all the necessary parts, you may break it into pieces (e.g. using String.prototype.split()
) and build up anew in desired order and with necessary delimiters:
const dateStr = '2020-06-21T10:15:00Z', [yyyy,mm,dd,hh,mi] = dateStr.split(/[/:\-T]/) console.log(`${dd}-${mm}-${yyyy} ${hh}:${mi}`)
.as-console-wrapper{min-height:100%;}
Converting ISO 8601-compliant String to java.util.Date
Unfortunately, the time zone formats available to SimpleDateFormat (Java 6 and earlier) are not ISO 8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC # 822.
Even if Java 7 added support for time zone descriptors according to ISO 8601, SimpleDateFormat is still not able to properly parse a complete date string, as it has no support for optional parts.
Reformatting your input string using regexp is certainly one possibility, but the replacement rules are not as simple as in your question:
- Some time zones are not full hours off UTC, so the string does not necessarily end with ":00".
- ISO8601 allows only the number of hours to be included in the time zone, so "+01" is equivalent to "+01:00"
- ISO8601 allows the usage of "Z" to indicate UTC instead of "+00:00".
The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z")
will give you a Calendar
object and you can simply use getTime() on it, if you need a Date
object.
You could probably use Joda-Time as well, but I don't know why you should bother with that (Update 2022; maybe because the entire javax.xml.bind
section is missing from Android's javax.xml
package).
Date conversion from yyyy-MM-dd HH:mm:ss to ISO date yyyy-MM-dd'T'HH:mm:ssXXX format issue
As by documentation of SimpleDateFormat:
For formatting, if the offset value from GMT is 0, "Z" is produced. If
the number of pattern letters is 1, any fraction of an hour is
ignored. For example, if the pattern is "X" and the time zone is
"GMT+05:30", "+05" is produced.
So my guess is probably to check the timezone of your server. Since it thinks that the timezone of the entered date is GMT 0.
How to convert ISO date format into 'MM/DD/YYYY' in bootstrap/angular
The perfect solution would be to alter the file below, I have edited the code to get the required format(DD-MM-YYYY) in my input field.
ngb-date-parser-formatter.js
from the code below you will get DD-MM-YYYY Date format in input filed.
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { padNumber, toInteger, isNumber } from '../util/util';
/**
* Abstract type serving as a DI token for the service parsing and formatting dates for the NgbInputDatepicker
* directive. A default implementation using the ISO 8601 format is provided, but you can provide another implementation
* to use an alternative format.
*/
var NgbDateParserFormatter = (function () {
function NgbDateParserFormatter() {
}
return NgbDateParserFormatter;
}());
export { NgbDateParserFormatter };
var NgbDateISOParserFormatter = (function (_super) {
__extends(NgbDateISOParserFormatter, _super);
function NgbDateISOParserFormatter() {
return _super !== null && _super.apply(this, arguments) || this;
}
NgbDateISOParserFormatter.prototype.parse = function (value) {
if (value) {
var dateParts = value.trim().split('-');
if (dateParts.length === 1 && isNumber(dateParts[0])) {
return { year: toInteger(dateParts[0]), month: null, day: null };
}
else if (dateParts.length === 2 && isNumber(dateParts[0]) && isNumber(dateParts[1])) {
return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: null };
}
else if (dateParts.length === 3 && isNumber(dateParts[0]) && isNumber(dateParts[1]) && isNumber(dateParts[2])) {
return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: toInteger(dateParts[2]) };
}
}
return null;
};
This is Where the date format is done
NgbDateISOParserFormatter.prototype.format = function (date) {
return date ?
(isNumber(date.day) ? padNumber(date.day) : '')+"-"+ (isNumber(date.month) ? padNumber(date.month) : '') +"-"+ date.year:'';
};
return NgbDateISOParserFormatter;
}(NgbDateParserFormatter));
export { NgbDateISOParserFormatter };
//# sourceMappingURL=ngb-date-parser-formatter.js.map
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'));
Convert ISO date to yyyy-mm-dd format
Consider running an aggregation pipeline that will allow you to flatten the data list first, project the new field using the $dateToString
operator, then regroup the flattened docs to get your desired result.
The above can be shown in three distinct pipelines:
db.users.aggregate([
{ "$match": { "username": "xyz" } },
{ "$unwind": "$followers" },
{
"$project": {
"username": 1,
"count": "$followers.count",
"date": { "$dateToString": { "format": "%Y-%m-%d", "date": "$followers.ts" } }
}
},
{
"$group": {
"_id": "$_id",
"username": { "$first": "$username" },
"followers": { "$push": {
"count": "$count",
"date": "$date"
}}
}
}
])
With MongoDB 3.4 and newer, you can use the new $addFields
pipeline step together with $map
to create the array field without the need to unwind and group:
db.users.aggregate([
{ "$match": { "username": "xyz" } },
{
"$addFields": {
"followers": {
"$map": {
"input": "$followers",
"as": "follower",
"in": {
"count": "$$follower.count",
"date": {
"$dateToString": {
"format": "%Y-%m-%d",
"date": "$$follower.ts"
}
}
}
}
}
}
}
])
how to Convert Date into ISO Date Format in javascript
You can use String.split()
to get the day, month and year for the Date in question.
We can then pass to the Date.UTC()
function and then the Date()
constructor. (Note: We pass monthIndex to the Date constructor, that's why we subtract 1 from the month )
To display as an ISO string, we can then use Date.toISOString()
const [month, day, year] = '05/23/2022'.split('/');
const date = new Date(Date.UTC(year, month - 1, day));
const result = date.toISOString();
console.log('Date (ISO):', result);
Related Topics
How to Run HTML Files Directly from Github, Instead of Just Viewing Their Source
HTML Dom: Which Events Do Not Bubble
JavaScript on the Bottom of the Page
How to Disable Div Element and Everything Inside
Make Checkbox Behave Like Radio Buttons with JavaScript
How to Submit a Form When the Return Key Is Pressed
Dynamically Populating Drop Down List from Selection of Another Drop Down Value
Insert External Page HTML into a Page HTML
How to Develop Desktop Apps Using HTML/Css/Javascript
How to Create a Responsive Image That Also Scales Up in Bootstrap 3
Download Textarea Contents as a File Using Only JavaScript (No Server-Side)
How to Use Google.Maps.Event.Trigger(Map, 'Resize')
Scroll to Element on Click in Angular 4
Class Methods as Event Handlers in JavaScript