Transform date to wire format (YYYY-MM-DD) in javascript - javascript

Having this input: Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time), is there a way to format it as YYYY-MM-DD, to it will become 2021-02-03?

Try this:
const date = moment(new Date('Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)')).format('YYYY-MM-DD');
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>

Parsing a timestamp to a date to reformat it may well produce incorrect results if the timestamp includes an offset or timezone. Given "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)", for a user with an offset of +1 or less the date will be 2 Feb, not 3 Feb.
The most reliable method is to reformat the string, e.g.
let timestamp = "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)";
let months = [,'Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
let pad = n=>('0'+n).slice(-2);
let p = timestamp.split(' ');
console.log(`${p[3]}-${pad(months.indexOf(p[1]))}-${p[2]}`);

Related

How can I convert my date "Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time) " to JAN 01,2020 using JavaScript?

Currently I am getting my date from data base in the format of Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time) but I want the date in JAN O1,2020 format and I don't want to use mon=ment.js for this. any other ways to achieve this?
Here is a solution without moment.js
var n = new Date("Wed Jan 01 2020 00:00:00 GMT+0530 (India Standard Time)");
var options = {
month: "short",
day: "numeric",
year: "numeric",
timeZone: 'IST'
}
document.write(n.toLocaleString("en-EN", options));
You can simply use moment.js
import * as moment from 'moment';
moment('your_date_variable').format('DD/MM/YYYY');

Create date from number of days

I am trying to convert the number of days since Jan 01 1970 to JavaScript Date.
Here is the code snippet.
new Date(864e5 * parseInt(data[i].d));
//here data[i].d contains number of days.
I checked all the data by this.
console.log(typeof(data[i].d), data[i].d);
//prints
number 17674
but sometimes it unable to convert it into date.
Invalid Date {}
while for
number 17858
//outputs.
Fri Aug 17 2018 05:00:00 GMT+0500 (Pakistan Standard Time)
Thanks for your time.
You just have to add the number of days times the milliseconds in a day, like so:
var originalDay = new Date(864e5)
console.log(originalDay) //Thu Jan 01 1970 19:00:00 GMT-0500 (Eastern Standard Time)
var numOfDays = 7
var daysSince = new Date(864e5 + parseInt(numOfDays * 864e5))
console.log(daysSince) //Thu Jan 08 1970 19:00:00 GMT-0500 (Eastern Standard Time) --7 days later
To make this work for you, you would just have to replace that numOfDays with the values in your array.

Date Formatting and filtering in angularjs

I want to convert "Mon Oct 12 2015 00:00:00 GMT+0530 (IST)" date format to "YYYY/MM/DD" in my controller.
Try to do this in this way:
var date = "Mon Oct 12 2015 00:00:00 GMT+0530 (IST)";
var newDate = $filter('date')(new Date(date), 'yyyy/MM/dd');
Angular date

Javascript vs Date

I have some interesting problem and can't find the solution. Look at this:
var d1 = new Date("07 31 2014");
document.write(d1);
document.write('<br />');
var d2 = new Date(1406746800 * 1000);
document.write(d2);
when I run this script I get this result:
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
then after I changed my time zone I get this result:
Thu Jul 31 2014 00:00:00 GMT-0800 (AKDT)
Wed Jul 30 2014 11:00:00 GMT-0800 (AKDT)
as you can see the second result is Jul 30, but first result is Jul 31. I think they must both be equal to 31 Jul. I know this problem depends on the timezone but is there a solution?
So the constructor parameter is:
an Integer value representing the number of milliseconds since 1
January 1970 00:00:00 UTC
So given your integer value, that represents (for me, in BST):
Wed Jul 30 2014 20:00:00 GMT+0100
Which is
Wed Jul 30 2014 19:00:00 UTC
And your timezone is GMT-8, so thats the above -8 which gives:
Wed Jul 30 2014 11:00:00 GMT-0800 AKDT
The date string constructor constructs the date in your local timezone. You can see what the value should be by doing this:
new Date("07 31 2014").getTime() / 1000
Which returns 1406761200, not 1406746800

Converting a date string into UTC+0530 format using javascript

I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?
Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.
I tried this code and it returned proper date (In Indian Locale)
var d=Date.parse("14,Feb,2011");
document.write(new Date(d));
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .
Here's an example of converting between different time zones.
<html>
<body>
<script type="text/javascript">
//Set you offset here like +5.5 for IST
var offsetIST = 5.5;
//Set you offset here like -8 for PST
var offsetPST = -8;
//Create a new date from the Given string
var d=new Date(Date.parse("14,Feb,2011"));
//To convert to UTC datetime by subtracting the current Timezone offset
var utcdate = new Date(d.getTime() + (d.getTimezoneOffset()*60000));
//Then cinver the UTS date to the required time zone offset like back to 5.5 for IST
var istdate = new Date(utcdate.getTime() - ((-offsetIST*60)*60000));
//Then cinver the UTS date to the required time zone offset like back to -8 for PST (Canada US)
var pstdate= new Date(utcdate.getTime() - ((-offsetPST*60)*60000));
document.write(d);
document.write("<br/>");
document.write(utcdate);
document.write("<br/>");
document.write(istdate);
document.write("<br/>");
document.write(pstdate);
</script>
</body>
</html>
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time)
Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.
var d = new Date("14-Feb-2011");
this will give an output of
Mon Feb 14 2011 00:00:00 GMT-0500 (Eastern Standard Time)

Categories

Resources