I have a value JUN 16 as string. I need to convert it to a date object. I need to find out last date of the month JUNE year 2016.
This is how I am converting a Date("MMM-YY") to Date("YYYY-MM-DD")
Input : Nov-17
run : new Date("Nov-17")
Output : Sat Nov 17 2001 00:00:00 GMT+0530 (India Standard Time)
now by adding a prefix to your date like below
input : 1-Nov-17 //add prefix to your string '1-'
run : new Date("1-Nov-2017")
output: Wed Nov 01 2017 00:00:00 GMT+0530 (India Standard Time)
Moment
run : moment("1-Nov-17").endOf('month').format("YYYY-MM-DD") //End day of the month
output: "2017-11-30"
I have a value "JUN 16" as string .I need to convert to date object
This is how you do it:
var date = new Date(2016, 5, 16);
console.log(date);
i need to find out last date of the month JUNE year 2016
And this is how you can do that:
// 5 is June
var date = new Date(2016, 5 + 1, 0);
console.log(date.getDay())
This tells you that the day is 4 which is "Thursday"
Related
i am struggling with my code, new Date() convert my value to next day 1 hour
new Date('2013-03-27T23:59:59.999Z') // => Thu Mar 28 2013 00:59:59 GMT+0100
As a Solution:
new Date('2013-03-27T23:59:59.999Z'.replace(/-/g, '\/').replace(/T.+/, '')) // => Wed Mar 27 2013 00:00:00 GMT+0100
Any suggestions how to get the correct date?
I'm confusing about the javascript Date.toISOString() function which shown as below example, how come date value of x in ISO format become January?
const date = new Date();
const x = (new Date(date.getFullYear(), date.getMonth() , 1));
console.log(date); \\Tue Feb 04 2020 11:11:12 GMT+0800 (Malaysia Time)
console.log(x); \\Sat Feb 01 2020 00:00:00 GMT+0800 (Malaysia Time)
console.log(date.toISOString()); \\2020-02-04T03:11:12.330Z
console.log(x.toISOString()); \\2020-01-31T16:00:00.000Z
This is due to time zone conversion from GMT+08 to UTC. The toISOString function converts the date to UTC (as a note you can determine that the date is in the UTC time zone by "Z" at the end of the string).
When converting Feb 01 2020 00:00:00 GMT+0800 to an ISO string, the date is reduced by 8 hours and hence becomes Jan 31 2020 16:00:00.
I am trying to convert a date in format momentjs into a date from javascript native new Date().
The problem is that if I have moment(myDay).toDate(); it converts to the current date, and I want the date from myDay.
myDay looks like: "YYYY-MM-DD" => 2017-11-24 and I would like to have it with the format: Fri Nov 24 2017 20:17:11 GMT+0100 (Hora estándar romance) but I get Thu Nov 16 2017 etc...
It is possible to convert it like that way?
Don't need moment:
let [yr, mn, day] = myDay.split('-').map(Number);
// note that JS months are 0-11 not 1-12
let datestr = new Date(yr, mn - 1, dy).toString();
console.log(datestr); // "Fri Nov 24 2017 00:00:00 GMT-0500 (EST)"
you want something like this:
moment(myDay, "YYYY-MM-DD").toString();
moment().toString() Returns an english string in a similar format to JS Date's .toString().
moment().toString() // "Sat Apr 30 2016 16:59:46 GMT-0500"
Following up from my previous post: Javascript Safari: new Date() with strings returns invalid date when typed
I am using Moment.js to convert a date string into a date field based on user input in the text box.
This is to prevent the problem I described in the linked post for Safari and Firefox not able to render the date when Chrome is fine.
Here is the code snipper:
var tempDate = moment(userInputFieldDate).format('DD-MM-YYYY');
alert(tempDate);
In Chrome, it does work fine (it use to work with the Javascript Date object too) but gives me the moment.js deprecation warning
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
Arguments: [object Object]
Error
On Firefox and Safari is just gives an UNDEFINED DATE in the alert window. So not entirely sure what should I be doing to convert the date string to a Date object.
Any suggestions on this issue?
If you are getting a JS based date String then first use the new Date(String) constructor and then pass the Date object to the moment method. Like:
var dateString = 'Thu Jul 15 2016 19:31:44 GMT+0200 (CEST)';
var dateObj = new Date(dateString);
var momentObj = moment(dateObj);
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
In case dateString is 15-07-2016, then you should use the moment(date:String, format:String) method
var dateString = '07-15-2016';
var momentObj = moment(dateString, 'MM-DD-YYYY');
var momentString = momentObj.format('YYYY-MM-DD'); // 2016-07-15
Sweet and Simple!
moment('2020-12-04T09:52:03.915Z').format('lll');
Dec 4, 2020 4:58 PM
OtherFormats
moment.locale(); // en
moment().format('LT'); // 4:59 PM
moment().format('LTS'); // 4:59:47 PM
moment().format('L'); // 12/08/2020
moment().format('l'); // 12/8/2020
moment().format('LL'); // December 8, 2020
moment().format('ll'); // Dec 8, 2020
moment().format('LLL'); // December 8, 2020 4:59 PM
moment().format('lll'); // Dec 8, 2020 4:59 PM
moment().format('LLLL'); // Tuesday, December 8, 2020 4:59 PM
moment().format('llll'); // Tue, Dec 8, 2020 4:59 PM
if you have a string of date, then you should try this.
const FORMAT = "YYYY ddd MMM DD HH:mm";
const theDate = moment("2019 Tue Apr 09 13:30", FORMAT);
// Tue Apr 09 2019 13:30:00 GMT+0300
const theDate1 = moment("2019 Tue Apr 09 13:30", FORMAT).format('LL')
// April 9, 2019
or try this :
const theDate1 = moment("2019 Tue Apr 09 13:30").format(FORMAT);
I want to convert date to millisecond as per follow I converted.
var d = new Date(1454911465467) \\ output : Mon Feb 08 2016 11:34:25 GMT+0530 (IST)
Now I want to convert using output to millisecond.
var d = new Date('Mon Feb 08 2016 11:34:25 GMT+0530 (IST)').getTime() \\output : 1454911465000
Expected output : 1454911465467
Is their any way to convert these type of millisecond?
Milliseconds are not specified in 'Mon Feb 08 2016 11:34:25 GMT+0530 (IST)'. The date precision here is down to seconds. Hence 467 milliseconds are missed in the second result.
You can check e.g.
var originalDate = new Date(1454911465467);
var clonnedDate = new Date(originalDate.getFullYear(), originalDate.getMonth(), originalDate.getDate(), originalDate.getHours(), originalDate.getMinutes(), originalDate.getSeconds(), originalDate.getMilliseconds());
document.write(clonnedDate.getTime());