I am getting date format like this /Date(1495111091673)/.I have created one custom filter to change date format.
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1);
}
})
This filter returns date like this.
Thu May 18 2017 18:08:11 GMT+0530 (India Standard Time)
But I want it as standard format like dd/MM/yyyy so I have edited my filter code like this:
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1, 'dd MMMM # HH:mm:ss');
}
})
Is it correct?
This filter returns date like this
Thu May 18 2017 18:08:11 GMT+0530 (India Standard Time)
No it doesn't, that's just how your console (or whatever) is choosing to display the Date instance (via Date.prototype.toString()).
I'd just use AngularJS's date filter ~ https://docs.angularjs.org/api/ng/filter/date.
For example (where dateFormat is your "/Date(1495111091673)/" formatted string)
{{dateFormat | jsonDate | date : 'shortDate'}}
Or in JS
let parsed = $filter('jsonDate')(dateFormat)
let dateString = $filter('date')(parsed, 'shortDate')
or via DI
.controller('controllerName', ['dateFilter', 'jsonDateFilter',
function(dateFilter, jsonDateFilter) {
let dateString = dateFilter(jsonDateFilter(dateFormat), 'shortDate')
}])
Related
I have a format which returns me some dates and I need to parse it into something else which I find a little bit complicated.
The data format is Mon Dec 24 2018 9:00:00 as a startDate for example and Friday Dec 28 2018 17:00:00 as an endTime for example. What happens here is that I select I want someone to start on Monday at 9 until 17 everyday, but what my data does is makes it look like he's working non-stop.
I have tried mapping over it and putting it onto objects with days of the week and start and end times, but I ran into a problem because I create the object like
dates : {
monday: {
start: 9:00,
end: 18:00
},
tuesday: {
start:9:00,
end:18:00
}
// etc for everyday of the week
}
But, what if I only need Monday through Thursday, for example, that would be a problem. Anyone has any idea, how could I do that, in any other way? I was thinking about using moment.js.
I'm not sure if I understand your question, but I think you want to list all days between two different dates, here is an example of function that iterates over days by using moment.isSameOrBefore and moment.add functions, hope this help:
function toDays(startDateString, endDateString) {
const startDate = moment(startDateString, 'dddd MMM DD YYYY');
const endDate = moment(endDateString, 'dddd MMM DD YYYY');
const dates = {};
while(startDate.isSameOrBefore(endDate, 'day')) {
const currentDay = startDate.format('dddd');
dates[currentDay] = {start:'9:00', end:'18:00'};
startDate.add(1, 'days');
}
return dates;
}
const result = toDays('Monday Dec 24 2018', 'Friday Dec 28 2018');
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
console.log("pre : "+vm.dailyCheckIn);
console.log(vm.temp_date.setHours(0,0,0,0));
console.log("next : "+vm.dailyCheckIn);
can someone help me with this code.
Result:
before temp variable changed (original date value)
pre : Mon Oct 29 2018 16:37:24 GMT+0530 (India Standard Time)
after temp variable changed (original date value)
next : Mon Oct 29 2018 00:00:00 GMT+0530 (India Standard Time)
It seems like that you have used the same date object in the temporary and in the actual variable. You have to create a new date object for the temporary variable.
e.g
var date = new Date();
var vm = {
dailyCheckIn: date,
temp_date: new Date(date) //Create a new date object
};
console.log("pre : "+vm.dailyCheckIn);
console.log(vm.temp_date.setHours(0,0,0,0));
console.log("next : "+vm.dailyCheckIn);
I hope it will help to you.
I have a JSON data in the below format and want to display the startDateTime in human readable format.
var txns = {
"payeeAccNum": "12081031991",
"startDateTime": "2018-11-13T20:47:47.866"
}
I have done in the following way, but it always showing time as 12:00:00 AM only. can any one help on this?
<span [innerHTML]="(txns.startDateTime | date:'d MMM yyyy h:mm:ss a')"></span>
Using the date filter you can format your date/time to any format you need to:
'medium': equivalent to 'MMM d, y h:mm:ss a' for en_US locale (e.g. Sep 3, 2010 12:05:08 PM)
'short': equivalent to 'M/d/yy h:mm a' for en_US locale (e.g. 9/3/10
12:05 PM)
'fullDate': equivalent to 'EEEE, MMMM d, y' for en_US locale
(e.g. Friday, September 3, 2010)
'shortDate': equivalent to 'M/d/yy' for en_US locale (e.g. 9/3/10)
'mediumTime': equivalent to 'h:mm:ss a' for en_US locale (e.g.
12:05:08 PM)
'shortTime': equivalent to 'h:mm a' for en_US locale
(e.g. 12:05 PM)
Now what format do you want your time to be displayed in?
for Medium display, use:
<span [innerHTML]="(txns.startDateTime | date:'medium')"></span>
and it's better if you use [innerText] in stead of HTML for span contents.
to get something like medium date without seconds use 'MMM d, y h:mm a'
Dateoffset filter:
transform(value: any, format = 'MM/dd/yyyy'): string {
let formattedDt: string;
if (value) {
if (DateUtil.DATE_TIMEZONE_REGEX.test(value)) {
value = this.removeTimezone(value);
}
formattedDt = this.datePipe.transform(new Date(value), format);
}
return formattedDt;
}
private removeTimezone(value: any): string {
const timezoneDashIndex = value.lastIndexOf('-');
return value.slice(0, timezoneDashIndex);
}
consider using moment.js library for datatime conversions
visit momentjs.com
for your code this is the solution:
var requiredDateFormat= moment(tsnx.startDateTime).format('MM-DD-YYYY')
I receive a number representing a date in ddmmyy format eg.: 250615 i need transform into this format : 25/06/15.
I canot use the angular date filter because i receive a number.
I try to use slice in this way but only print the first number
{{ singularitem.Dia.slice(0,2)}} {{ singularitem.Dia.slice(2,2) }} {{ singularitem.Dia.slice(-2,2) }}
How can it be done ?
ThankĀ“s in advance
Since you can't use the date filter, just create your own filter, doing whatever you want it to do:
app.filter('transformDate', function() {
return function(date) {
// Split string at every second character and combine them again with a `/` in between
return date.match(/.{1,2}/g).join('/');
}
})
Usage:
{{date | transformDate}}
Plunker: http://plnkr.co/edit/uBeT0eHOoAlHDUWydOpM?p=preview
You can go for a vanilla javascript function. If your format is always dd/mm/yy check this function:
(function () {
var date = "250615";
date = date.slice(0, 2) + "/" + date.slice(2,4) + "/" + date.slice(4,6);
console.log(date);
})()
FIDDLE
Slice is a prototype function of Strings and Arrays, not numbers. You need to convert your number to string first.
If you want to convert that number to a date object, you'd need:
function parseDate(dateAsNumber) {
var dateAsString = "" + dateAsNumber;
var day = Number(dateAsString.substr(0, 2));
//months are index 0
var month = Number(dateAsString.substr(2,2)) - 1;
//assumming all dates are 2000+ since you only get 2 digits
var year = Number(dateAsString.substr(4,2)) + 2000;
var date = new Date(year, month, day);
return date;
}
Calling parseDate(250615) returns Thu Jun 25 2015 00:00:00 GMT-0700 (Pacific Daylight Time)
I've tried the angular docs of {{someDate | date:'params'}}, however; This doesn't work if someDate is in this format yyyy-MM-dd HH:mm:ss.
How would I convert yyyy-MM-dd HH:mm:ss to say just yyyy using angularjs.
The date filter is designed to handle multiple input types including date objects and common date strings. If your date string is not recognized by the provided date filter, I would just write my own filter. It would look something like this...
myapp.filter('toYear', function () {
return function (dateString) {
var dateObject = new Date(dateString);
return dateObject.getFullYear();
};
});
You can use it like this...
{{someDate | toYear}}
You can define a custom format as follows
Date.prototype.customFormat = function(){
return this.getMonth() +
"/" + this.getDate() +
"/" + this.getFullYear();
}
//usage
> var d = new Date()
Fri Mar 14 2014 15:39:07 GMT+0000 (GMT Standard Time)
> d.customFormat()
'2/14/2014'
More details https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
I actually fixed the solution here:
app.filter('timestampToISO', function() {
return function(input) {
input = new Date(input).toISOString();
return input;
};
});
http://bit.ly/1iJAV8G
UPDATE:
To implement you add this to the view:
{{someSQLTIMESTAMP | timestampToISO | date}}
Angular js date object doesn't support to convert from MySql date format to timestamp directly. So need to convert it to acceptable format from YYYY-mm-dd H:i:s to YYYY/mm/dd H:i:s and then convert it to a timestamp.
You may try the below snippet to convert Mysql date format YYYY-mm-dd H:i:s to timestamp using AngularJs filter
app.filter("mysqlDateFormatToTimestamp", function(){
return function(date){
var date1 = date2 = date3 = timestamp = hours = minutes = seconds = '';
date1 = date.split(':');
date2 = date1[0].split(' ');
date3 = date2[0].split('-'); // Change it based on your format
if( date1.length == 1 && date2.length == 1 ){
hours = '00';
minutes = '00';
seconds = '00';
}else{
hours = parseInt(date2[1]);
minutes = parseInt(date1[1]);
seconds = parseInt(date1[2]);
}
timestamp = new Date(parseInt(date3[0]), parseInt(date3[1])-1, parseInt(date3[2]), hours, minutes, seconds);
return timestamp;
}
});
Now, You may use this filter wherever the date format you want, and you may change the params as you like.
{{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'LLLL dd, yyyy HH:mm:ss'}}
// results would comes like "July 07, 2016 05:27:30"
{{ '2016-07-07 05:27:30' | mysqlDateFormatToTimestamp | date:'yyyy'}}
// results would comes like "2016"