I am using luxon library to convert the time:
const DateTime = luxon.DateTime;
console.log(DateTime.local('Sun Jan 23 2003 00:00:00 GMT+0200 (Eastern European Standard Time)').toISODate())
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
I expect to get this format: 2003-04-23 Why i get null and how to get the expected format using luxon?
You have the date format wrong, Luxon’s format that belongs in .local is year?, month, day, hour, minute, second, millisecond
Example:
const DateTime = luxon.DateTime;
console.log(
DateTime.local(2003, 1, 23, 17, 36) .toISODate())
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
For your case you can use the code below to get what you are looking for.
const DateTime = luxon.DateTime;
const date = new Date("Sun Jan 23 2003 00:00:00 GMT+0200 (Eastern European Standard Time)")
const dat = DateTime.fromJSDate(date)
console.log(dat.toFormat('MM-dd-yyyy'))
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
can be done with pure date
var date =new Date("Sun Jan 23 2003 00:00:00 GMT+0200 (Eastern European Standard Time)")
const DateTime = luxon.DateTime;
var dat = DateTime.fromJSDate(date)
console.log(dat.toString().split("T")[0])
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
Related
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]}`);
I am currently using moment js in my application and not able to figure out how to convert epoch time to GMT date time. Providing my code below:
click: (event) => {
console.log(moment.utc(event.point.category).toDate());
}
event.point.category is providing epoch time which I want to convert to GMT date time object, but above code is converting it to local timezone date object.
For ex. 1606262400000 is getting converted to Tue Nov 24 2020 19:00:00 GMT-0500 (Eastern Standard Time) instead of GMT date time which is Wednesday, November 25, 2020 12:00:00 AM
You should get the (UTC) offset (in minutes) of the client to correct time
click: (event) => {
const offset = new Date().getTimezoneOffset();
const targetTime = moment.utc(event.point.category).toDate();
const offsetTime = new Date(targetTime.getTime() + offset * 60 * 1000);
console.log(offsetTime);
}
I believe all that you are missing is a toUTCString().
An epoch timestamp will be the same for Tue Nov 24 2020 19:00:00 GMT-0500 (Eastern Standard Time) and Wednesday, November 25, 2020 12:00:00 AM GMT
click: (event) => {
console.log(moment.utc(event.point.category).toDate().toUTCString());
}
To remain as a Date object, use moment.utc().format()
You can try something like this,
click: (event) => {
console.log(moment(moment.utc(event.point.category).toISOString()).utc());
}
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"
I have this yest_date variable in javascript
var yest_date = "Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)"
I want this variable 'yest_date' to be in this format and the value is.
20161212
Can someone let me know how to achieve this.
I would suggest use moment.js. It is a very good library to handle any date time related problem http://momentjs.com/
var yest_date = moment("Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)")
console.log(yest_date.format("YYYYMMDD"))
If you don't want to add an extra library then you can use the classic string concat
let yest_date = new Date("Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)")
console.log(`${yest_date.getFullYear()}${yest_date.getMonth() + 1}${yest_date.getDate()}`)
Simply convert your string into an actual Date, then use the Date getter methods to extract the values you want into a formatted string:
let yest_date = "Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)"
let date = new Date(yest_date);
console.log(`${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`)
You can do the following:
const date = new Date('Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)')
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
const formattedDate = `${year}${month}${day}`;
console.log(formattedDate);
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)