AngularJS format date doesn't format - javascript

{{ map.thedate }}
The above outputs 2014-06-29 16:43:48
When I try the below code it still shows the same date as above.
{{ map.thedate | date:'medium' }}

your date is not in ISO format. Use a filter to convert your input to a date and then apply the date filter
app.filter("toDate", function () {
return function (input) {
return new Date(input);
}
});
Then in html markup:
{{map.thedate | toDate | date:'medium'}}

Conver your date, here is an example:
for (var i=0; i<map.length; i++) {
var unixTime = (new Date(map[i].thedate)).getTime();
map[i].thedate= unixTime;
}
AngularJS will not accept that format of date, this is an easy solution to iterate over your dataset and convert it to a format it will accept.
Another alternative is to do the same thing about on the server-side. Just iterate over it and convert it to a unix timestamp.
This is a description from the docs. on what it will accept:
Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.

Related

How to convert RFC 3339 datetime into regular JavaScript format

I need to use the Google Calendar service for editing single instances of an event series. The service returns datetime values in RFC 3339 format (like 2022-11-03T21:30:41.043Z), which is hard to process within Google apps script. Is there an easy way feeding it into Utilities.formatDate to convert it (ie yyyy-MM-dd HH:mm)?
Vice versa is plainly done with .toISOString().
You can convert an ISO8601 format date string into a JavaScript Date object directly with the Date constructor. To convert the Date object into a formatted date string, use Utilities.formatDate() with your preferred timezone, like this:
function test() {
const dateISO8601 = '2022-11-03T21:30:41.043Z';
const timezone = 'PST';
const dateString = toDateString_(dateISO8601, timezone);
console.log(dateString);
}
function toDateString_(dateISO8601, timezone = 'GMT') {
const date = new Date(dateISO8601);
return Utilities.formatDate(date, timezone, 'yyyy-MM-dd HH:mm');
}

How to convert to date-fns?

I have following moment expression:
<DialogContent>
{startDate ? (
<DateTimePicker
value={startDate}
onChange={(value: any) =>
setStartDate(moment(value).format())
I would like to convert to date-fns format, but it fails, it is always 1970 something, why? I converted like this:
setStartDate(getUnixTime(new Date(value)))
You can import format function from date-fns and use it:
import { format } from 'date-fns';
// Pass desired format as a second parameter
setStartDate(format(value, 'yyyy-MM-dd'));
moment(value).format() return a ISO 8601 date string, look like: 2021-11-10T10:19:09+09:00. Then, I guess the setStartDate function require a date string with ISO 8601 format.
The goal is using date-fns to create a string as the result of moment.format.
To do that, you need to create a date object. If it works with moment(value) syntax, mean value is a millisecond timestamp number (1636507295498) or a formal date string (2021-11-10). Then you can create a date object by Date constructor:
new Date(value);
Now, you can use formatISO to create the required date string:
formatISO(dateObject);
The final look will be like this:
setStartDate(formatISO(new Date(value)))

Specific date format and optional time included with moment.js

Team,
I have got a Typescript method like below.
public getDateDisplayFormat(dateFormat: string, displayTime: boolean): string {
}
The "dateFormat" parameter can be of any with/without date format like MM/DD/YYYY or DD/MM/YYYY or DD/MM/YY HH:mmss Z or MM/DD/YY HH:mmss Z or any other valid formats.
But based on the displayTime parameter I need to add/remove time value in the date.
I tried L and LLT formats but those are giving just MM/DD/YYYY irrespective of my date format.
The dateFormat is dynamic based on the client and so I cannot change it from DD/MM into MM/DD.
So, my dateFormat which is passed should remain same but only the time portion should add/remove in the format.
Please suggest how I can achieve this.
Thanks in advance.
Try this
public getDateDisplayFormat(dateFormat: string, displayTime: boolean): string {
let format = dateFormat.substr(0,str.indexOf(' ')); //get the format by splitting the string on first space. Date goes at index 0, rest goes at index 1.
if(displayTime){
//if format length is greater than 1, it means that dateFormat had both date and time format so return it, else, append time format and return it.
dateTime = format.length > 1 ? dateFormat : format[0] + "your time format";
return dateTime;
}
// if time format is not needed just return the date format.
return format[0];
}

JavaScript input date convertion

Is there any way to convert any date string (not necessarily current date) (could be any format) to specific date format in Javascript. Like converting "MM-DD-YYYY" or "ddMMYYYY" to "DD-MMM-YYYY"?
I know that from current date as var date = new Date(), we can get time and hours but what to do in case of existing date string like "31/01/1999" to "31-JAN-1999".
Given the input date string can be of any format.
This is a common problem.
You should be able to do it with moment.js.
Ex.
moment("31/01/1999").formatWithJDF("dd - MM - yyyy");
Have a look here, https://momentjs.com/docs/ for more details.
Using DateFormatter.js
var date = new Date('2020-03-25 10:30:25');
var formatter = new DateFormatter();
displayFormat = 'D M d Y h:i:s';
var dateString = formatter.formatDate(date, displayFormat); // Wed Mar 25 2020 10:30:25
This can be done by first checking the input date format with the arrays or Map of regex provided, then need to convert that format to JS accepted format ISO format.
Once converted to ISO format this date now can be converted to any form with logic written for it in separate functions.

Angular formating yyyy-MM-dd HH:mm datetime string

I am struggling with some datetime formating in angularJS.
I would like to obtain the following date format 25/11/1990 14:35
based on a datetime string containing "1990-11-25 14:35:00" without having to create a Date object in the controller.
It seems like angular can only achieve proper datetime formatting if the input is a Date object or if the String does not contain hours or minutes
index.html
<div ng-app ng-controller="Ctrl">
String: {{'1990-11-25 14:35:00' | date:"dd/MM/yyyy HH:mm"}}<br>
string date: {{'1990-11-25' | date:"dd/MM/yyyy"}}<br>
Date: {{myDate | date:"dd/MM/yyyy HH:mm"}}
</div>
controller.js
function Ctrl($scope)
{
$scope.myDate = new Date("1990-11-25 14:35:00");
}
Output
string: 1990-11-25 14:35:00
string date: 25/11/1990
date: 25/11/1990 14:35
http://jsfiddle.net/CkBWL/612/
According to angular's documentation on the date filter,
the only format allowed for datetime string are:
datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ
Is there so a way to format a string containing "1990-11-25 14:35:00" into 25/11/1990 14:35 directly in the html without having to create a Date object ?
Thanks for your help !
It seems like angular can only achieve proper datetime formatting if the input is a Date object or if the String does not contain hours or minutes
If you want your string recognized as a date you should use the date formatted according to the ISO8601 standard. So 1990-11-25 14:35:00 becomes 1990-11-25T14:35:00Z, you can also include the offset if you want (its in the spec). You can then use the existing date filter built into Angular.
From the date filter documentation on param date
Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.
Code update
<div ng-app ng-controller="Ctrl">
String: {{'1990-11-25T14:35:00Z' | date:"dd/MM/yyyy HH:mm"}}
<br>
Date: {{date | date:"dd/MM/yyyy HH:mm"}}
</div>
function Ctrl($scope) {
$scope.date = "1990-11-25T14:35:00Z";
}
Updated JsFiddle

Categories