I have a javascript string in the following format:
2016-06-22T14:47:29.689358
How would I use ng-moment to parse the string into a moment object and then format it inside my view?
https://github.com/urish/angular-moment
Pseudocode:
$scope.time = "2016-06-22T14:47:29.689358";
<span am-time-ago="time | amParse:'YYYY.MM.DD HH:mm:ss'"></span>
It seems like time needs to be converted into a Date object before it is passed to ng-moment.
You don't need a date at all, and in fact you shouldn't use the date object's parser as it behaves in odd ways. You just have the wrong format specified for the date you have.
<span am-time-ago="time | amParse:'YYYY-MM-DDTHH:mm:ss.SSS'"></span>
That should be all you need.
For more information about why dates parse unreliably, you can see this question.
Looking into the documentation you can go with
amFromUnix filter: Converts a unix-timestamp (seconds since 1/1/1970) into a moment object. Example:
<span am-time-ago="message.unixTime | amFromUnix">
To get the unixTime from your date string just go with the following:
var unixTime = new Date("2016-06-22T14:47:29.689358").getTime();
Looks like from it just wants a moment object.
$scope.time = moment("2016-06-22T14:47:29.689358");
Related
I want to manipulate date which come from the api.
When I use: console.log(dataAPI.dateStation)
I see 2023-01-24T06:00:00.000Z
Is there way to change the date time in this format 2023-01-24 06:00:00
Just I want to remove T character between date and time and remove .000Z at the end.
The simplest way to do it is probably:
new Date(dataAPI.dateStation).toLocaleString()
If what you want is to display it somewhere, it'll automatically adapt the ISO date you have into a localized and readable date (based on timezone and language).
To know more about it and the options, here is the doc: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
If you want to print the date in ISO 8601 format, you can use the 'sv' (Sweden) locale and Date.toLocaleString().
You can also specify whichever IANA timezone you wish to use, I'm using UTC in this case.
const d = '2023-01-24T06:00:00.000Z'
let timestamp = new Date(d).toLocaleString('sv', { timeZone: 'UTC' });
console.log('Timestamp:', timestamp);
Use the javascript date class with toLocaleString
new Date(dataAPI.dateStation).toLocaleString('en-US');
Without installing some third-party library, your best bet is probably to use the string you have (which is the format returned by toISOString() ),and modify it as desired. If it's already a string in the format you gave, you can just call replace on it:
dataAPI.dateStation.replace('T',' ').replace('.00Z','')
If it's a Date object, first call toISOString() to get a string:
dataAPI.dateStation.toISOString().replace('T',' ').replace('.00Z','')
If it's a string in a possibly-different format, call new Date() to get a Date object, then call toISOString() on that, and finally call replace on the result:
new Date(dataAPI.dateStation).toISOString().replace('T',' ').replace('.00Z','')
You can use regular expressions to remove the parts you don't want:
let s = "2023-01-24T06:00:00.000Z"
s = s.replace(/T/, ' ')
s = s.replace(/\.\d{3}Z$/, '')
console.log(s)
I am using date js to quickly parse any string into a date and it is working perfectly. However I need to also parse a timestamp.
var temp_string = "1484120122526";
var date = new Date(temp_string);
It gives back
NaN –
Regular javascript Date object does this, but I can't find a way for datejs to do it. And since it overwrites the Date object, I am stuck. Can datejs parse timestamps? or is there a way for me to call new Date() and reference the original date object?
Even though dateJS does indeed overwrite the default js Date class, the fault was on my side, I did not see in the documentation the fact that timestamp has to be an int, as I was looking everywhere in the dateJS documentation not the Date one.
so the code should be:
var temp_int = 1484120122526;
var date = new Date(temp_int);
I want to show in a view of an angular project a human friendly date format, and I saw that angular has a filter to do so, but the input date needs to have a certain format.
The desired output format to be seen in the view is the following: "dd/MMMM/yyyy hh:mm:ss"
Currently, I have on a Database the following timestamp format:
"2016-08-15 12:34:34"
How can I format this type of timestamp so that angular can intepret it and format it as desired?
Thanks a lot!
You can parse the date to a javascript date and then use the date filter in Angular to show it in the format you want (if you can't use the date filter on the date from your database directly).
You could do this as a filter to make it easier:
myApp.filter('formatted', function() {
return function(value) {
return new Date(value);
}
});
In your html you could then add this filter before the date filter:
Filter: {{ vm.dateToFormat | formatted | date: 'dd/MMMM/yyyy hh:mm:ss' }}
If Angular is having trouble parsing the string from the database on its own you could just use JavaScript Date.parse("2016-08-15 12:34:34") to return you a date that Angular will interpret properly.
You could just use the inline filter:
Example:
<span>{{vm.Date | date: 'dd/MMMM/yyyy hh:mm:ss'}}</span>
EDIT: To be clear, you post doesn't clarify if that timestamp is a string or not, so you may have to parse it accordingly. I see the other posters just assumed it was a string.
I am looking for a way to get minutes only from a date in string (coming from toISOString).
When using Date object, I was using getTime(), but dont think there is a direct method available for ISO format.
Would I need to extract strings directly from ISO format, as its just a string?
Code:
var depTime = new Date(1222332000).toISOString();
This gives me "1970-01-15T03:32:12.000Z", so what is a good way to get minutes which is "32".
You can use getMinutes() from the date object:
var d = new Date('1970-01-15T03:32:12.000Z');
console.log(d.getMinutes());
This is the right method, but, if there are issues with the Time Zone, you can parse the string:
var depTime = new Date(1222332000).toISOString();
console.log(depTime.split(":")[1]); // 32
You can use
deptime.getMinutes(); // 32
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMinutes
Since it's just a string, regex should work just fine.
T\d+:(\d+)
I want to create a Date object in Javascript using this string 04/21/2014 12:00p
When passed to the constructor (new Date('04/21/2014 12:00p')), it returns Invalid Date.
I've seen other posts which manipulate the string in order to fulfill the requirements of a valid dateString, however that is not what I want. I want Javascript to recognize my date format (m/dd/yy h:mmt). In Java, something like that is simple, I imagine that there would be a similar way in Javascript.
How can I get the Date object to recognize my format?
This is trivial only when using a library like moment.js:
var dt = moment("04/21/2014 12:00p","MM/DD/YYYY h:mma").toDate();
Otherwise, you would have considerable string manipulation to do. Also you would have to account for users in parts of the world that use m/d/y or other formatting instead of the y/m/d formatting of your input string.
If this string is being sent from some back-end process, you might consider changing the format to a standard interchange format like ISO-8601 instead. Ex. "2014-04-21T12:00:00"
To manipulate the string in order to fulfill the requirements, could be a way, but you need to take care of all browser issues.
A more quick and dirty way is use moment.js library. It helps on formatting matters too.
if (String.prototype.dateFromJava == null)
{
String.prototype.fromJava = function (sDateString)
{
var aDateOrTime = sDateString.splt(" ");
var aDateParts = aDateOrTime[0].split("/");
var aTimeParts = aDateOrTime[1].split(":");
var oDate = null;
/* just get the pieces and passing them in to new Date(), return oDate */
}
}