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"
Related
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 need to conver my ison date formate to .net json date formate in javascript
like this
Tue Nov 26 2019 16:00:00 GMT-0800" to "/Date(1574812800000)/"
With JavaScript:
var dt = new Date("Tue Nov 26 2019 16:00:00 GMT-0800");
var format = "/Date("+dt.valueOf()+")/";
console.log(format);
You can use Moment.js:
moment("Tue Nov 26 2019 16:00:00 GMT-0800", "ddd MMM D YYYY H:m:s [GMT]ZZ").format("[/Date(]x[)/]")
You should get the result:
/Date(1574812800000)/
The timestamp you supplied corresponds to May 23, 2001 at midnight GMT.
I'm trying to write an if statement that runs some code if the date is after April 24th, 2017, 10 am EDT, but it doesn't appear that my variables are comparable (different data types?).
I'm trying to avoid using Moment.js for just this.
var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';
today returns Tue Apr 04 2017 14:34:41 GMT-0400 (EDT).
When I test if either is greater than the other, both are false. How should I format my dates?
Thanks!
You have to have launch as a date type:
var today = new Date();
var launch = 'Mon Apr 24 2017 10:00:00 GMT-0400 (EDT)';
var launchDate = Date.parse(launch);
if ( launchDate > today )
You should also read more about dates here:
Compare two dates with JavaScript
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"
I have date in 23-08-2015 00:00:00 format in my controller and pass it to view using ViewData. I want to convert this date to Tue Aug 23 2015 00:00:00 GMT+0530 format.. Is it possible with controller or can I convert it in my view using jquery?
Can anyone help me to solve this?
In javascript, if you simply create a new Date object with the string that you have, you'll get the desired format.
var testdate = new Date("23-08-2015 00:00:00");
console.log(testdate);
Output:
Tue Nov 08 2016 00:00:00 GMT+0530 (India Standard Time)
In your controller:
DateTime date = DateTime.Now;
date.ToLongDateString();
I done it by using following format:
string startDateCalendar = Convert.ToString(startDate.ToString("ddd MMM dd yyyy HH:mm:ss")) + " GMT+0530";
using JavaScript it is like so:
function parseDDMMYYYY(d){
return new Date(
d.replace(/^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/,"$3-$2-$1 $4:$5:$6")
)
}
parseDDMMYYYY('23-08-2015 00:00:00')