I have checked this SO post: Where can I find documentation on formatting a date in JavaScript?
Also I have looked into http://home.clara.net/shotover/datetest.htm
My string is: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)
And I want to convert it to dd-mm-yyyy format.
I tried using:
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDay()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
But it gives me the result as: 1-6-2013
The getDay() value is the index of day in a week.
For Instance,
If my dateString is Thu Jun 20 2013 05:30:00 GMT+0530 (India Standard Time)
it gives output as 4-6-2013
How can I get the proper value of Day?
P.S: I tried using .toLocaleString() and creating new date object from it. But it gives the same result.
To get the day of the month use getDate():
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
W3 schools suggests just building your days of the week array and using it:
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var n = weekday[d.getDay()];
Not super elegant, but usable.
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
Replace getDay() with getDate().
The above will return the local date for each date part, use the UTC variants if you need the universal time.
I think you will have to take an Array of the days & utilize it using the received index from the getDay() method.
To get required format with given date will achieve with moment.js.
a one liner solution is
import moment from "moment";
const date = new Date();
const finalDate = moment(date).format("DD-MM-YYYY")
Related
I am trying to change an ISO date to a Standard JS Date format. The JS format I am referring to is:
Mon `Jul 20 2020 14:29:52 GMT-0500 (Central Daylight Time)`
What is the best way to go about doing this? Thanks!
const ISO_DATE = '2020-07-14T23:02:27.713Z';
function formatDate(dateStr) {
const date = new Date(dateStr);
return date.toString();
};
console.log(formatDate(ISO_DATE));
One way is:
let isoDate = "2020-07-20T14:29:52Z";
var myDate = new Date(isoDate);
console.log(myDate.toString()); // Mon Jul 20 2020 17:29:52 GMT+0300 ( (your time zone)
console.log("Back to ISO Date: ", myDate .toISOString());
If you want to convert it back to ISO Date use:
console.log(myDate.toISOString());
I am attempting to convert the following into a 12 hour am/pm format.
Currently I am recieving the Day, Month, Year and timezone.
Fixed by adding .toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.)/, "$1$3")*
<div id="time1"></div>
<div id="time2"></div>
var date = new Date('08/16/2019 12:00:00 PM UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time1").innerHTML = date;
var date = new Date('08/16/2019 6:00:00 am UTC').toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
document.getElementById("time2").innerHTML = date;
Basically what you have to do is use the Date() default javascript function and make sure you append the UTC timezone:
var date = new Date('08/16/2019 7:00:00 PM UTC')
date.toString=() //will then print out the timezone adjusted time
"Fri Aug 16 2019 22:00:00 GMT+0300 (Eastern European Summer Time)"
There are many built in javascript methods to handle converting date objects. This example will look to the browser to determine date format and time.
let time = Date.now();
time.toLocaleDateString();
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 am at (UTS-05:00) Eastern Time (US & Canada)
i.e, new Date().getTimezoneOffset() == 300 seconds.
Now, I have a API endpoint (JSON) that returns a date string like this.
{
someDate: '2016-01-01T00:40:00.000+00:00'
}
Here, I pass it to Date constructor like this
var dateString = "2016-01-01T00:40:00.000+00:00";
var someDay = new Date(dateString);
console.log(someDay)
Mozilla Firefox console shows
Date {Fri Jan 01 2016 00:40:00 GMT-0500 (Eastern Summer Time)}
Google Chrome console shows
Thu Dec 31 2015 19:40:00 GMT-0500 (Eastern Standard Time)
Chrome is taking the TimezoneOffset into consideration and Firefox is not. What can I do to get a Date that doesn't take Offset into consideration like FireFox in Chrome?
You can do it by:
var dates = '2016-01-01T00:40:00.000+00:00'.split(/-|T|:/);
var newDate = new Date(dates[0], dates[1]-1, dates[2], dates[3], dates[4]);
This hack works (not very clean, but does the job)
var dateString = '2016-07-27T01:40:30';
var dateParts = dateString.split(/-|T|:/);
var saneDate = new Date(
+dateParts[0],
dateParts[1] - 1,
+dateParts[2],
+dateParts[3],
+dateParts[4],
+dateParts[5]);
console.log(saneDate);
I would like to compare the given date in the below format in JaveScript. I have tried the following,
Thu May 19 2016 00:00:00 GMT+0530 (India Standard Time)
Thu May 20 2016 00:00:00 GMT+0530 (India Standard Time)
var ExpiryDate = userAccount.ExpiryDate();
var datetoday = new Date();
var Expired = (DateTime.Compare(ExpiryDate, datetoday) == -1 ) ? true : false;
//if expiry date is less than today date then var expired should be true
But didn't worked. I could not compare those two dates. It results in un handled exception. Is there any other way to do this date comparison in JaveScript ?
I have referred the following answers in SO but they are in different date format. So that I have raised this question,
javascript compare two dates and throw an alert
Javascript comparing two dates has wrong result
Compare two dates in JavaScript
Javascript compare two dates to get a difference
Any suggestion would be helpful.
var date = new Date();
//# => Fri May 20 2016 16:09:43 GMT+0530 (India Standard Time)
var date2 = new Date();
date2.setDate(date.getDate() - 1);
//# => Thu May 19 2016 16:09:43 GMT+0530 (India Standard Time)
date > date2 //# => true
use getTime()
var date1 = (new Date("20 May 2016")).getTime();
var date2 = (new Date("19 May 2016")).getTime();
date1>date2
You will find some good method here