Use Moment to get month of Date object - javascript

I'm sure this is a simple thing but I haven't been able to find the specific syntax in any of the documentation or in any related posts.
In order to get a month-picker to work i need to instantiate a new Date object when my controller initializes.
Controller
scope.date = new Date();
This creates a date object with the following format:
Mon Feb 01 2016 15:21:43 GMT-0500 (Eastern Standard Time)
However when I attempt to pull the month from the date object, using moment, I get the error:
enter code here
getMonth method
var month = moment().month(scope.date, "ddd MMM DD YYYY");
Any idea how to pull the month from the above date object without using substring?

You can use moment.month() it will return or set the value.
moment.month() is zero based, so it will return 0-11 when doing a get and it expects a value of 0-11 when setting passing a value in.
var d = moment(scope.date);
d.month(); // 1
d.format('ddd MMM DD YYYY'); // 'Mon Feb 01 2016'

As moment.month() returns zero based month number you can use moment.format() to get the actual month number starting from 1 like so
moment.format(scope.date, 'M');

You can use moment(scope.date).format("M");

Related

moment.js uses the month as day instead the day

I'm using currently moment.js (moment-with-locales.js - v.2.14.1) in my project. I want to remove the time of my datetime string to get only the date. But if I use the .format() method of moment.js I got an incorrect date.
I want to format this datetime string:
from ' 08.10.2016 11:00 ' to ' 08.10.2016 '
Here is a snipped that I used in my angular project:
var date = moment('08.10.2016 11:00').format('DD.MM.YYYY')
console.log(date)
If I run this I got this output
10.08.2016
instead of
08.10.2016
The funny thing is, if I want to get the timestamp (milliseconds) of my datetime string, it works perfect. Example:
var dateStart = moment('08.10.2016 19:00', 'DD.MM.YYYY HH:mm').valueOf()
console.log(dateStart)
Will return
1475946000000 -> Sat Oct 08 2016 19:00:00 GMT+0200
How can I get the correct Date?
It depends on your locale. en-US locate means moment will parse by "month day year". So, you need to parse with the pattern as well:
var date = moment('08.10.2016 11:00','DD.MM.YYYY HH:mm').format('DD.MM.YYYY')

Convert Date and Time to Current Millis (JavaScript)

I'm using the Angular UI Bootstrap Datepicker and Timepicker (https://angular-ui.github.io/bootstrap/), and I want to combine both of those values and get the current millis. I get the date in this format: "2015-05-19" [YYYY-MM-DD"] and the time as this: "10:57:19" [HH:MM:SS].
I saw that there is a Date.parse() method where you can get current millis but I couldn't find a way where you can include a time as well, and was wondering if there was a method for this?
Since you already have a date and a time, you could use these to construct a date string:
var date = '1970-01-01';
var time = '00:00:00';
var dateString = date + 'T' + time; // '1970-01-01T00:00:00'
var parsedDate = Date.parse(dateString); // 0 Milliseconds
The 'T' is for handling timezones. Since I'm located in central Europe, my timezone offset is GMT+1 (Central European Time).
var newDate = new Date(parsedDate); // Thu Jan 01 1970 01:00:00 GMT+0100 (CET)
looks like the answer would be: Date.parse('MM DD YYYY HH:MM:SS');
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
but you probably need to be wary of the time zone.

Date object using string

I have a string date coming from a server and wanted to see if there was a way to get a JS date object from it.
Example: format mm/yy
exDate = "0919"
I want to return a new date object with the last day of the month also added to it. So for example the above would return
Mon Sep 30 2019 00:00:00 GMT-0500 (EST)
Is the above possible with just a 2 digit year and returning the last day in the month with the date object?
Parse in the string to a date object specifying the month ahead and 0 for the date:
var t = new Date("20" + exDate.substring(2, 4), exDate.substring(0, 2), 0);

JavaScript Changes My Date Based on Browser Time Zone

I am trying to create a Date object in JavaScript, passing a string like this:
2014-11-30T00:00:00.0000000
However, the value of the Date object is:
Sat Nov 29 2014 17:00:00 GMT-0700 (Mountain Standard Time)
It changed it to 11/29 when I want 11/30. Is there any way I can make the date 2014-11-30, regardless of what time zone the browser is in?
Note: One possible workaround is to use the Date(year, month, day) constructor; however, I am constructing the data in a JSON string, which doesn't appear to support this.
EDIT:
Actually, I just did a test and created a date using Date(2015, 1, 1) and it gives me:
Mon Feb 02 2015 00:00:00 GMT-0700 (Mountain Standard Time)
So I can't even create a date that way and have it be the date I want. I don't understand why this is so difficult.
You can use Date.UTC
The UTC() method differs from the Date constructor in two ways.
Date.UTC() uses universal time instead of the local time.
Date.UTC() returns a time value as a number instead of creating a Date
object.
EDIT - why does SO insist on making links so hard to spot? That, up there, is a link to the docs in case that wasn't obvious.
EDIT 2 - I think I misunderstood. Try this:
var d = new Date('2014-11-30T00:00:00.0000000');
var utc = new Date(
d.getUTCFullYear(),
d.getUTCMonth(),
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds()
);
alert('d: ' + d + "\n" + 'utc: ' + utc);

How to take only part of the javascript timestamp

I have a js timestamp of Tue Sep 30 2014 12:02:50 GMT-0400 (EDT)
with .getTime() I got 1412092970.768
for most cases, its a today's specific time stamp. I wonder, if I could always ONLY pick out the day month and year and hour, min, day will be always stay with 0.
So for our situation, it should become Tue Sep 30 2014 00:00:00 GMT-0400 (EDT).
I wonder what kind of conversion should I be doing? Because seem convert to unix timestamp with getTime() will result in unknown way of calculation... and I can not really find a way to set time like I would do in PHP.
Any fix for this situation?
Thanks
You can create a date object and then zero-out any components you don't need, or create one with the components you specified, e.g.
foo = new Date();
foo.setHour(0);
foo.setMinute(0);
or something more like
foo = new Date(); // "now"
bar = new Date(foo.getYear(), foo.getMonth(), foo.getDate(), 0 , 0, 0, 0);
// create new date with just year/month/day value, and time zeroed-out.
The constructor's args are detailed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
An other option is to send epoch to PHP:
JS:
long epoch = System.currentTimeMillis()/1000;
PHP:
$dt = new DateTime("#$epoch");
$dt->format('Y'); //year

Categories

Resources