How to format dates from server into JS - angularjs

It might be a super simple solution, but I am struglling with it for a while now.
I am using angular.js and using input time.
The server returns the following date convention:
shiftStarted = "2017-01-17 19:55:11"
I want to put it as ng-model in the input field:
<input type="time" ng-model="shiftStarted" />
The problem is that input time should receive a valid JS date format,
In order to try to solve it I used this solution I found on the web:
shiftStarted = new Date(shiftStarted.replace(' ', 'T'));
But the following is changing the timezone to UTC.
How can I simply put the date accurately in the input time field without handling tough timezone issues?
Best,
Omri

If your date has been formatted as YYYY-MM-DD, the browser will parse it as an ISO date. (the full format would be 2017-01-17T18:03:26+00:00 at the time of me writing this)
Since no timezone portion was provided (only a date), the browser assumes the timezone is UTC by default.
If you just want to parse a date without bothering with timezones, you could use MomentJS (as suggested by Ananth)
Code would be:
shiftStarted = moment(shiftStarted, "YYYY-MM-DD").toDate();

There is the angular-date directive:
<input type="text" datetime="yyyy-MM-dd HH:mm:ss" ng-model="shiftStarted">
This also include a parser service. New timezone additions too.
https://github.com/eight04/angular-datetime

You could try date.getTimezoneOffset()

Related

Statsig issue with Before Time and After TIme

I need to configure Statsig flag to return true for 30 days, by setting from and to dates
I am using "check time and date" and I am trying to set before and after time. But its giving invalid error. Not sure what is the mistake. Please guide to resolve this issue
Statsig uses American formats for dates - so MM/DD/YYYY, and XX:XX AM/PM
I think the UI should have a datepicker which enforces this. Here's what mine looks like

Format time of input type time

this is my input:
<input
type="time"
className="start-time"
name="busi_start_time"
// value="Message"
/>
the screen will display as hh:mm (12h). How can I format it as HH:mm (24h)?
It is not possible to customize the display format of the HTML input time as you can see in the documentation:
[...] uses a 12- or 24-hour format for inputting times, based on system locale
The behavior of the HTML input is defined by the browser and it seems that all browsers use the OS locale format to choose which display format to show to the user. So seeing a 12h or 24h format will depend on which is set on your OS.
A potential solution to force a display format is to use a library, but I guess that is outside of the scope of your question.

Adjusting the time zone when using momentJs with React

I am fetching a datetime value from the back end and I use the following line of code to get the time apart from the date :
{moment(element.dateTime).format('h:mm:ss a')}
I just want to know how to adjust the time zone in the code above to be the same as my country's time zone. Is there a way to achieve this?
Thanks in advance.
Moment by default will parse the date to your local computer. https://momentjs.com/guides/#/parsing/local-utc-zone/
You can use the utcOffset method as below, it takes the GMT Offset value. Change '+05:30' to whatever value you want
{moment.utc(element.dateTime).utcOffset("+05:30").format('h:mm:ss a')}
Also, Moment by default parses the date to your local timezone.
You can also use moment-timezone for timezone purposes, it asks for timezone name instead of gmtOffset. If you wish to use format method of moment though, there are certain limitations to it.
Moment Timezone

MomentJs/Angular Date filter not working in IE

I've my date format as '2017-05-16T13:22:01.207Z' and trying to convert to '05/16/17 08:22 AM'. I've tried the following and none of them worked for me in IE, works fine in chrome. Any ideas what is the reason. Did anyone ever face this situation.
Date = '2017-05-16T13:22:01.207Z';
MomentJs Solution:
$moment(Date).format('MM-DD-YY HH:mm A').replace(/-/g,"/")
AngularJs Date Filter:
{{Date | date: 'short'}}
Expected Result: I'm seeing this in chrome, but not in IE
05-16-17 13:22 PM
Hehe, the replace might be the funniest way I've seen someone handling the format() function, which already formats a date as one wishes.
Use
var date = '2017-05-16T13:22:01.207Z';
alert(moment(date).format('MM/DD/YY HH:mm A'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
Also note!!!
Date is already defined in js (it's the global Date object); so you'd be better off using some different name for your variable - like the similar sounding date; but with a lowercased D/d. (This might be where your error comes from)

Convert datetime in decimal trough angularjs

I would like to ask you, how can I convert decimal number to data in angularJS?
source:
20150911141804
expected:
2015-09-11
I need format this source number inside cellTemplate in ui-grid.
Thanks
If you need to display something you're getting from a data source, like a JSON file or a $http.get(...), then using a filter is the way to go, but I suggest you build a custom one, seeing your format is quite uncommon.
If you need to parse a value entered by a user in, say, an input text, then the right way is to use a custom parser.
If you want to know more you can read the official docs for filters and parsers.
use
<span>{{1288323623006 | date:'yyyy-MM-dd'}}</span><br>
refer to
https://docs.angularjs.org/api/ng/filter/date
Your date has a very strange format.
If you cannot change it (to an UNIX timestamp for example), consider converting your date (as a String) to something like:
20150911141804 => '2015-09-11'
And then create a new date from this String :
var date = new Date('2015-09-11');
Once you have a real Date object, you can format it using angular's native date filter or momentjs.

Resources