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];
}
Related
I'm trying use DatePipe for HH:MM:SS (or any other documented formats) just as I've used DecimalPipe for monetary values.
However, passing my logic a format such as hh:mm:ss my values immediately turn to 07:00:00.
time = 'hh:mm:ss';
val = '1:02'; // user entered value
invalids = new RegExp('[^0-9:]{0,6}', 'g'); // time chars only
replaced = String(val).replace(invalids, ''); // remove non-hhmmss characters
let value = this.datePipe.transform(replaced, time); // format the value
console.log(value); // '07:00:00' why!?
Here is the repro in StackBlitz:67
How can I get the component to accept those predefined formats to force the input value?
Angular DatePipe's input is typed as any, but actually requires a Date, or miliseconds, or ISO formatted string which you are not giving it.
https://angular.io/api/common/DatePipe#input-value
value any The date expression: a Date object, a number (milliseconds
since UTC epoch), or an ISO string
(https://www.w3.org/TR/NOTE-datetime).
The data you are passing in does not meet that criteria so it's probably not going to work for you use case unless you can modify it before sending to datePipe.
I need to format this value in milliseconds "1543325996" to date like this "18/01/1970, 11:42:05 PM". I've already got the right result using 'toLocaleTimeString' function, but this result has String type. I need exactly Date type.
function dateFormat(date) {
var formDate = new Date(+date).toLocaleDateString("en-GB");
var formTime = new Date(+date).toLocaleTimeString("en-US");
var concatDate = (formDate + ", " + formTime);
// here I've got error 'Invalid Date'. I know that it's a wrong way, but don't know what to do.
var newDate = new Date(concatDate);
return newDate;
}
but this returns error "Invalid Date". Is there another way to convert String to Date?
...but this result has String type. I need exactly Date type.
Date objects don't have a format. Formatting is intrinsically a textual thing (e.g., string).
If you want Dates, then new Date(+date) is giving you that. There's nothing further required. Later, at some point, if you want to display that date in a textual form, use toLocaleDateString or Intl.DateTimeFormat or similar to format them in the way you want them formatted. But not until/unless you need to convert them to text (a string).
{{ 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.
I have a date stored in variable like below
var date = moment(new Date()).valueOf();
I need to format it like below
CCYY-MM-DDThh:mm:ss[.sss]TZD
The Time Zone Definition is mandatory and MUST be either UTC (denoted by addition of the character 'Z' to the end of the string) or some offset from UTC (denoted by addition of '[+|-]' and 'hh:mm' to the end of the string).
I have tried like below
var required = moment.utc(date).format('CCYY-MM-DDThh:mm:ss[.sss]TZD')
But it is resulting like below
"CC14-06-03T07:59:15.sssT+00:003"
But the expected format examples are
UTC :
1969-07-21T02:56:15Z
Houston time :
1969-07-20T21:56:15-05:00
Just try with:
'YYYY-MM-DDThh:mm:ssZ'
Output:
2014-06-03T08:16:15+00:00
moment.js format documentation
Code:
new Date(moment('14/07/2022 14:27:50', 'DD/MM/YYYY HH:mm:ss').format('YYYY-MM-DDTHH:mm:ss'))
Output:
2022-07-14T17:27:50.000Z
I'm using custom plugin for jQuery UI datepicker - Timepicker
I have two fields, from and to input field, and need to check if to "is greater than" from. I have my custom onClose function (onClose: function(dateText, inst) {}), but the first parameter is value of input - a date string. But my date string is not in the "valid" JS datetime format so I'm not able to get Date object instance and compare.
It's dd.mm.yyyy hh:mm, e.g. 06.08.2012 12:00
I wonder if there is anything how to specify input string format, e.g.:
var date = new Date('dd.mm.yyyy hh:mm', dateText);
If not I'll have you parse it somehow...
Thanks for help in advance.
There is no such utility built in to JavaScript. If I were you I would match the date format with a regex and use the form of the Date constructor which accepts date parts:
function parseDate(str) {
var m = str.match(/^(\d\d)\.(\d\d)\.(\d{4}) (\d\d):(\d\d)$/);
return (m) ? new Date(m[3], m[2]-1, m[1], m[4], m[5]) : null;
}
Note that the month part is zero based (instead of one based, so January=0, hence the minux one). Also, note that the Number constructor is used to convert strings to numbers so you don't have to worry about numbers possibly prefixed with a zero being interpreted as octal as can happen with parseInt(...).