How to change date format in Angularjs - angularjs

I am facing same problem, by using above mentioned code problem is still not resolved.
My date is coming in JSON like
200515
where 20= date
05= month
15= year,
I am using the following code:-
{{200515 | date : "EEE, MMM dd"}}
but it gives me:
Thu, jan 01
but I want
Wed, May 20
Please help for the same.

You have to create a date object before using your filter because it will only formatted the date. Here your date is null.
Firstly you have to create your date using basic Date api
new Date(year, month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
and after use the created object in your code with the filter

Your input string for your date 200015 is not a valid ISO 8601 date string. You must supply either a javascript Date object or a valid string of format such as 'yyyy-MM-ddTHH:mm:ss.sssZ' or 'yyyy-MM-ddTHH:mmZ' etc. See http://en.wikipedia.org/wiki/ISO_8601

Just use .substring() to parse your date string and build a new date object. This works with angular's date filter.
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
var date = '200515',
day = date.substring(0, 2),
month = date.substring(2, 4),
year = date.substring(4, 6),
time = '0513',
hour = time.substring(0, 2),
minute = time.substring(2, 4);
$scope.date = new Date('20' + year, month - 1, day, hour, minute);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp", ng-controller="MyCtrl">
{{date | date : "hh:mm EEE, MMM dd"}}
</div>

Related

Get month and date from React moment

I have a date on Wed Dec 25 2019 05:30:00 GMT+0530. I want to format into 25 Dec
How can I do that?
const date= currentItem.date;
First convert it into moment
let date_moment=moment(currentItem.date,"ddd MMM DD YYYY HH:mm:ss ZZ")
Then convert it to required format
let req_format=date_moment.format("DD MMM")
Note: if your timeformat is 12 hour use hh. for other datetime format and timezone format check moment documentation :) Moment Docs
This snippet of code will get exactly what you want from any date entered as variable name date to dateIn.
var dateIn = moment(date, 'YYYY/MM/DD');
var month = dateIn.format('M');
var day = dateIn.format('D');
console.log(day + " " + month);

How to parse date in input type="date"?

i have problem in angularjs when i use <input type="date" ng-model="trip.start"> i think data in trip.start is 2017-5-10 but data in trip.start is 2017-5-10T17:00:00.000Z
How to parse trip.start just yyyy-mm-dd without time.
please helpme if you have a idea to parse it.
I will suggest to use moment for date format. After getting data from server use:
$scope.trip.start = moment($scope.trip.start).format("YYYY-MM-DD");
For this to work, include Moment.js library.
The ng-model data must be a Date Object.
Then your input box will show the date.
If you want to show formatted date then you can do something like this-
$scope.formattedDate = $filter('date')($scope.trip.start, "your date format");
Add $filter as a dependency in your controller and bind $scope.formattedDate to your input box.
You can do,
var date = trip.start.split("T")[0]
you will get expected result.
I don't know how to completely parse it, but try date filter when you are using the data. Something like this
syntax:
{{ date_expression | date : format : timezone}}
in your case:
{{trip.start | date: 'yyyy-MM-dd'}}
For more reference read the documentation here
Your date value seems to be a JavaScript date. Angular already provides a filter for formatted date outputs:
{{trip.start | date:'yyyy-MM-dd'}}
Alternatively you could convert the date to string:
function formatDate(date) {
var date = date || new Date();
var today = new Date(date);
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
return (yyyy + '-' + mm + '-' + dd);
}

Why using Angular Moment returns all january in ng-repeat?

I am using
<div ng-repeat="usage in previousUsage"
<p>{{usage.Month | amDateFormat : 'MMMM' }}</p>
</div>
usage.Month data are numbers 1-12
The data returns January
amDateFormat expects to get a date as its value. Not necessarily a timestamp, but any kind of object that moment.js will be able to create a moment object from (Date object, string, timestamp in milliseconds, etc.).
When you use a number between 1-12 as your input, moment will see it (as #pavel-horal commented) as the amount of milliseconds that had passed since the beginning of 1970-01-01. Then you use the filter to display the month, and you'll get January.
Thanks guys,
I just did
<div ng-repeat="usage in previousUsage"
<p>{{dateFilter(usage.Month) | amDateFormat : 'MMMM' }}</p>
</div>
$scope.dateFilter = function(month) {
var objDate = new Date(month);
return objDate;
};
Something along these lines - it returned back as January, February, March, etc...

formatting yyyy/mm/dd in VB Script to Month name and day date

How can you format yyyy/mm/dd in VBScript to only Output Month name and day date 'Month Name' 'Day' eg. June 6
Sorry if this is a bad question - new to VBScript. My research has found only super lengthy solutions and just numeric formatting and rearranging, I need month name!
Recent attempt (please notice I'm trying to use (DatePart("m" but I also need to output the HTML etc I am writing within <section id='
<%
today = Date
firstDay = DateValue("2015-05-10")
lastDay = DateValue("2015-06-08")
d = firstDay
While d <= lastDay
If d = today Then
response.write "today"
Else
response.write(DatePart("m","<section id='day_"& d &"' class='calSquare " & time &"'><article class='dateImage' style='background-image: url(images/Calendar_Thumbnails/Day_"&d&".jpg)'></article></article><article class='dateTitle'> "&d&suffix&"</article></section>"))
End If
d = d + 1
Wend
<!--response.write(products(0))-->
%>
The above failed; also tried MonthName but couldn't use properly maintaining the html I'm populating.. Any suggestions as to how to output MonthName and then just numeric day date, maintaining my HTML I am populating dynamically as seen in above code?
You can use the MONTHNAME function. It takes a integer not a date so you could do something like the following.
MONTHNAME(MONTH("1/12/2015")) & " " & DAY("1/12/2015")
Output:
January 12
Use d.month to get the month and do.year to get the year. Convert the d.month to month name by creating a function to do so

Angularjs + moments-js returns wrong date

i use angularjs and i want parse a string to a date my code looks like this:
var d=moment.utc("2011-10-02T22:00:00.000Z", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
var year =d.year();
var month =d.month();
var day =d.day();
$log.log("MOMENTS:"+"year: "+year+" month: "+month+" day: "+day);
the date should be "3 october 2011"
but in the log is :MOMENTS:year: 2014 month: 2 day: 6
this date is completly wrong, why is this so and what do i wrong ? i want extract the day, month and year
Here is a fiddle: http://jsfiddle.net/FGa2J/
moment.day() returns the day of the week (not the day of the month) you need moment.date() for that. moment.month() is 0 based, so you will get 9 for October. Also, it seems like moment can parse your date string just fine without specifying the format.
Code from the fiddle:
report ( "2011-10-02T22:00:00.000Z", ["yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"] );
var d = moment("2011-10-02T22:00:00.000Z");
var year = d.year();
var month = d.month();
var day = d.date();
console.log("MOMENTS:"+"year: "+year+" month: "+(month+1)+" day: "+day);
function report( dateString, formats) {
$("#results").append (
$("<li>", { text:
dateString + " is valid: " + moment(dateString, formats).isValid()
})
);
}
You're not using the correct string formatting characters.
You have: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
In moment, it would be: "YYYY-MM-DDTHH:mm:ss.SSSZ"
Note that it's case sensitive, and doesn't necessarily match formats from other languages (.Net, PHP, etc).
However - since this is the ISO-8601 standard format, it is detected and supported automatically. You should simply omit that parameter.
From the documentation:
... Moment.js does detect if you are using an ISO-8601 string and will parse that correctly without a format string.

Resources