Displaying Day and Date from feed - javascript

How do I display the following as a a Day, Month?
"2013-05-28T00:00:00.0000000" as Tuesday, May 28 ?
thanks.

So since you have tagged the question as both javascript and python
I will recommend two libraries that solve the issue.
Using http://momentjs.com/ you can solve your problem as follows here
Using http://delorean.readthedocs.org you can solve your problem as follows here

If in python you can do it like this:
from datetime import datetime
datetime.strptime('2013-05-28T00:00:00.0000000','%Y-%m-%dT00:00:00.0000000').strftime('%A %b, %d')
>>>'Tuesday May, 28'
This will output in your current locale language.
Hope this helps!

If you want to do this is javascript it will look something like the following
var d = new Date();
d.toISOString();
document.write(d);

Related

How to turn string to date using Moment

I'd like to turn
2021030213000 +0000
into this format
1:00 pm
I'm using Moment.js in the project and I've tried to do it like so
Moment("2021030213000 +0000").format('LT')
But it returns invalid date.
Any help would be hugely appreciated!
So the problem here is that you are trying to invoke moment("dateStr") where your dateStr is not ISO-8601 compliant.
You can see the relevant API doc here: https://momentjs.com/docs/#/parsing/string/
You want to use moment's String+Format parser api in order to pass in a different format like the one you have above: https://momentjs.com/docs/#/parsing/string-format/
So for your case, it would look like:
moment("2021030213000 +0000", "YYYYMMDDHHmm +ZZ").format("LT")
Hello you could use somethings like this:
var date = moment("2021030213000 +0000", "YYYYMMDDhhmm").format('LT');
console.log(date);

How to format hour and minutes in date-fns?

I'm migrating my code from MomentJS to date-fns and having the following issue when setting the hour and minutes.
This is my momentJS that works just fine:
var someDate = moment.utc('2020-07-16T16:35:39.955873Z')) // 2020-07-16T16:35:39.955Z
console.log(someDate.format('MM/DD/YYYY [ at ] LT ')); // 07/16/2020 at 4:35 PM
This is my code using date-fns:
var someTime = zonedTimeToUtc('2020-07-16T16:35:39.955873Z', 'utc'); // 2020-07-16T16:35:39.955Z
console.log(format(new Date(someTime), "MM/dd/yyyy 'at' h:mm a")); // 07/16/2020 at 10:35 AM
so, I want my date-fns code to print
07/16/2020 at 4:35 PM
but it's printing
07/16/2020 at 10:35 AM
Why is that? A simple way to get it to print the date that I want is by removing the "Z" from the value of someTime variable (like this: 2020-07-16T16:35:39.955), then it works, but I don't want to remove it manually. Can anyone tell me what I'm missing or doing wrong? Thanks a lot in advance!
Here's a LIVE DEMO
Try using utcToZonedTime()
To change the displayed date/time returned from format(), you must use either:
utcToZonedTime: when you want to know what the local date is in another timezone
zonedTimeToUtc: when you want to know what a date in another timezone is in the local timezone
Working Demo

How to get hours in moment.js in GMT timezone?

For example if I do for the above date object something like: value.hours(), I get as output 16 instead of 18. I believe it returns the hours in the original GMT time, not like in my date object which is GMT+2. I can of course add 2 to the returned result, but it becomes cumbersome. Is there any way to get the hours correctly in my case?
I'm not sure as to what you've already tried, but I put the following into JSFiddle and it worked like a charm. I am currently in CST in America and it is 8:30 in the morning here. When I ran the snippet below I got today's date at 1:30 PM which I would assume is accurate in difference.
HTML
<div id="m1"></div>
JavaScript
var a = moment.tz(new Date(), "GMT");
document.getElementById('m1').innerHTML = a.format("YYYY MM DD; HH:mm");
The Moment.js documentation states the following in regards to creating a Moment object with a native JavaScript Date object:
You can create a Moment with a pre-existing native JavaScript Date object.
var day = new Date(2011, 9, 16);
var dayWrapper = moment(day);
This clones the Date object; further changes to the Date won't affect the Moment, and vice-versa.
To find the information quoted above quickly, when you reach the Moment.js documentation, it is located under the Parse section under sub-section Date.
To display local time:
value.local();
value.hours(); // 18
To reverse:
value.utc();
value.hours(); // 16
I think that you can solve it by doing what the docs says. Something like this:
moment().tz("America/Los_Angeles").format();
https://momentjs.com/timezone/docs/#/using-timezones/

How to add month to date in angularjs?

Hi I am developing web application in angularjs. I am developing one form and i have one textbox with date picker. I am using 720kb date picket pluggin. https://github.com/720kb/angular-datepicker I want users to allow dates after 1 month. For example if today is august 14 then i want to enable dates only after september 14. I have added moment.js and put
var date = moment();
date.add(1, 'months');
console.log(date);
$scope.maxdate=date.d;
In html i have date-min-limit="maxdate". How ever this is not going to work. I got date-max-limit="" method from the above mentioned document. May i know what i am doing wring in the above code? Can someone help me to fix this? Thank you.
date-min-limit="" expects a string version of short date
In your controller
$scope.maxdate = moment().add(1, 'M').format('MM/DD/YYYY');
In your html
date-min-limit="maxdate"
If you use bootstrap, you can use this pluggin https://github.com/uxsolutions/bootstrap-datepicker
In its example,demos, you can set start date.

Comparing 2 Times with DateJS

I've seen a few different questions on here regarding finding the difference between using two different dates.
My question is similar to Parse ONLY a time string with DateJS.
I basically have 2 time inputs:
<input id="start_time" type="text">
<input id="end_time" type="text">
The format of these will always be: 07:15 AM or 08:30 AM
Essentially, what I am trying to do is ensure the start_time is not greater than the end_time.
I have tried using DateJS to parse the date, but it returns null:
Date.parseExact("03:15 PM", "HH:mm"); <--- returns null
How should I go about comparing the two input fields (using DateJS or something else) to ensure the start_time is not greater than the end_time?
Any help would be great.
You need to add the AM/PM field to the format you're using to parse with. Try using
HH:mm tt
Instead of
HH:mm
Not exactly answering the question, but DateJS looks a bit outdated. I would suggest you take a look at Moment.js, where you can do this:
moment("03:15 PM", "hh:mm A").isAfter(moment("03:05 PM", "hh:mm A"));
// false
Why not using toString?
var g = new Date("2012-01-11 03:15 PM");
console.log(g.toString('HH:mm'));
just add a ficticious date in order to obtain a valid date string format.
Since I guess you just need the Time not the date
It works fine with me.
You can use Date.parseExact with the format revision presented by #matthewtole, or you should be able to just use 'Date.parse' as well.
Date.parseExact will provide better performance, but if you're just parsing two values, the performance improvement of Date.parseExact is probably not going to make much difference.
You can also use the .isBefore() function to check if one Date occurs before the other.
Example
var d1 = Date.parse("07:15 AM");
var d2 = Date.parse("08:30 AM");
d1.isBefore(d2); // true
Hope this helps

Categories

Resources