Date object using string - javascript

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);

Related

Cannot convert string to date - Invalid Date

I've this date: 16-08-13 I want convert it into DateTime 'cause I need to insert this value inside my database.
Actually I did this:
date = new Date('16-08-13');
but I get this error:
Invalid Date
From the Date documentation for the Date constructor taking a date string
dateString
String value representing a date. The string should be in a format
recognized by the Date.parse() method (IETF-compliant RFC 2822
timestamps and also a version of ISO8601).
From the spec, a 2-digit year won't cut it. You want YYYY-MM-DD at the minimum:
console.log(new Date('2016-08-13'))
Simply add the time element to the input string.
var date = new Date('2016-08-13 00:00');
Your date format is confusing because 13-08-16 could mean the 13th day of the 8th month of the 2016 or the 16th day of the 8th month of 2013
You just have to make the year 4 digits
date = new Date('2016-08-13')
that will give you this results
Date {Fri Aug 12 2016 19:00:00 GMT-0500 (Central Standard Time)}
which is a datetime object.

Use Moment to get month of Date object

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");

How does minus integer work in Date?

How does javascript Date interpret the milisecond integers?
var d = new Date(-1724115600000); //this gives me a date in the past, which I want
console.log(d);
var d = new Date(1724115600000);
console.log(d);
(we had a bug where the - sign was not getting through. But I dont understand the significance of the -)
The Date object constructor can take a variety of inputs, but when called in this fashion it's using the integer value one:
Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).
Negative values will give dates before the Unix Epoch, positive values are dates after the Epoch.
0 would be 1. January 1970. The delta is given as an unsigned number representing milliseconds. If you want dates before that you need to use negative values in milliseconds.
The negative number you provided will give a number in the past, the other one in the future:
Date 1915-05-14T23:00:00.000Z
Date 2024-08-20T01:00:00.000Z
If you got one in the past with the second number it may have been missing the last digit when your tried. In that case it would give:
Date 1975-06-19T12:06:00.000Z
var d = new Date(-1724115600000); //this gives me a date in the past, which I want
document.write(d + "<br>");
var d = new Date(1724115600000); //This gives me a date in the past too.
document.write(d + "<br>");
var d = new Date(172411560000); //missing last digit
document.write(d);
//negative sign give you the date before 1970. in your example
var d = new Date(-1425223942000);// this gives date in the past
document.write(d) //Sun Nov 02 1924 03:27:38 GMT-0500 (Eastern Standard Time)
document.write('<br/>')
var d = new Date(1425223942000); //This gives date in th future.
document.write(d); // Sun Mar 01 2015 10:32:22 GMT-0500 (Eastern Standard Time)
//Unfortunately i cannot post the screenshots yet

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

new Date(...) adds a month forward

I'm developing with node.js, and I'm trying to create a date object on the server.
when coding:
var birthyear = 2000;
var birthmonth = 7;
var birthday = 24;
var date = new Date(birthyear, birthmonth, birthday);
console.log(date);
OUTPUT:
Thu Aug 24 2000 00:00:00 GMT+0300 (Jerusalem Daylight Time)
As you can see, I'm getting August instead of July.
How can I fix that issue ?
The month argument in the Date() constructor doesn't start at 1 for January, but instead at 0. Therefore, supplying the month value of 7 gives you the eight month, which is August.
From MDN:
month: Integer value representing the month, beginning with 0 for January to 11 for December.
Months in JS start at 0
so it's a quite an easy fix:
var date = new Date(birthyear, birthmonth-1, birthday);
DEMO
Months in the JavaScript Date() constructor are 0-indexed, meaning that month 7 actually is August. Use the value 6 for July.
Annoyingly enough, Date is rather inconsistent about which fields are 0-indexed and which are 1-indexed, and in fact the same field can be either one depending on the context. You should always refer to documentation when you have to use dates.
Yea, that's weird, but months should be counted starting with
0

Categories

Resources