I have a date in local time zone which is Mon Jan 13 2020 00:00:00 GMT+0530 (India Standard Time).
I need this
Jan 13 2020
date in UTC. when i converted using
moment(
moment(start_date).format('mm-dd-yyyy'),
).Unix();
i am receiving a UNIX time stamp corresponding to the date Jan 12 2020 in UTC.
what i need is for Jan 13 2020 get a UTC timestamp of date Jan 13 2020 instead of getting Jan 12 2020. Time values are not important.
Get the UTC timestamp like this:
moment.utc('Jan 13 2020').format('X')
Above will give you the UTC timestamp in seconds.
If you want milliseconds, use x instead of X.
Breaking down your statement:
moment(start_date)
does not provide the parse format to moment.js so you will get a warning to that effect. In this case, moment.js will fallback to the built–in parser as if you'd written:
moment(Date.parse(start_date))
so you are hoping the built–in parser parses it correctly.
Then there is:
.format('mm-dd-yyyy')
which will return a date string in that format or Invalid Date if the parser couldn't parse the string.
However, likely you have used the wrong tokens so the result will be something like "30-mo-yyyy":
mm is two digit minute, if you want two digit month then use MM
dd is two letter day name, if you want two digit day in month use DD
yyyy is not a valid token, if you want the four digit year then use YYYY
So the format tokens should be:
.format('MM-DD-YYYY')
The result is then passed to moment, again without specifying the input format so again moment will show a warning and use the built–in parser again with the same potential for errors or invalid Dates. If parsed correctly, it will return a Date for the local timezone for the Date.
.Unix()
Is not a valid moment method, likely you want .unix(), which produces a UNIX timestamp (time value) for the equivalent UTC time. If the host system is set to +5:30 then the UTC time will be 5:30 earlier (and if it's before 5:30 am local, the date will be the day before).
If you want to parse the string correctly and get a date for the start of the day, then do:
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').startOf('day').unix()
Or create a formatted string however you like. A much more common format is YYYY-MM-DD, so consider:
let start_date = 'Mon Jan 13 2020 00:00:00 GMT+0530 (India Standard Time)';
// Get UNIX timestamp for start of day
console.log(
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').startOf('day').unix()
);
// Get formatted date string
console.log(
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').format('YYYY-MM-DD')
);
console.log(
moment(start_date, 'ddd MMM DD YYYY HH:mm:ss ZZ').format('DD MMM YYYY')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Related
Using moment how can we convert localized date string to javascript date object?
Followed below steps, but got Current Output(check below).
How can we get Expected Output?
Could you please help me.
Ex: Setting moment locale as 'fr'
moment.defineLocale('fr', [localeObject][1]);
Now we get the date string as "27 févr. 2020 18:23:50"
How can we convert it as dateobject?
moment("27 févr. 2020 18:23:50")
Expected output:
Thu Feb 27 2020 18:23:50 GMT+0530 (India Standard Time)
Current Output:
Invalid Date
You are getting Invalid Date because "27 févr. 2020 18:23:50" is not in ISO 8601 nor in RFC 2822 compliant form, so you have to use moment(String, String). Moreover you have to use french locale, see i18n section of the doc.
As described in the docs, you can use DD for days of the month, MMM for month name (locale aware), YYYY for 4 digit year, HH for 0-23 hours (lowercase hh for 1-12 hours), mm for minutes and ss for seconds.
Then you can use toDate() to get a JavaScript date from a moment object:
To get a copy of the native Date object that Moment.js wraps, use moment#toDate.
Snippet with working code sample:
console.log( moment("27 févr. 2020 18:23:50", "DD MMM YYYY HH:mm:ss").toDate());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/fr.js"></script>
I have used moment to convert date to ISO supported format as it's working fine on chrome & firefox but not on IE11 so
I as per the docs YYYY-MM-DD is supported iso format,
here my js_time1 format is as mentioned ddd MMM D YYYY hh:mm:ss
so i have used this to fomrat it
var js_time = moment(js_time1).format('YYYY-MM-DD');
i have also tried
var js_time = moment(js_time1,'ddd MMM D YYYY hh:mm:ss').format('YYYY-MM-DD');
but no use.
Format for js_time1 is Mon Aug 3rd 2018 12:12:21
Thanks in advance
Assuming js_time1 is what you've shown as "format for" ("Mon Aug 3rd 2018 12:12:21"), there are three problems:
August 3rd, 2018 was a Friday, not a Monday.
Your parsing format uses D for 3rd. D doesn't match ordinals, that's Do.
You're using hh (1-12) for hours, which is meant to be used with a. I assume your time is in 24-hour format, so that's HH.
It works if you fix those things (including fixing the error in the string):
var js_time1 = "Fri Aug 3rd 2018 12:12:21";
var js_time = moment(js_time1,'ddd MMM Do YYYY HH:mm:ss').format('YYYY-MM-DD');
console.log(js_time);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
I'm trying to extract month and year from a custom date string
Wed Dec 24 00:00:00 -0800 2014 using Moment.js.
I found this previous answer: How to parse given date string using moment.js? but the accepted answer is throwing an error.
Thought the syntax would be:
var date = moment("Wed Dec 24 00:00:00 -0800 2014","ddd MMM DD HH:MM:SS ZZ YYYY").format('dd');
console.log(date); expected output is 24 but instead returns Invalid Date.
Any ideas how to pull date out of the given date string? Thanks.
To anyone interested, the solution was making HH:MM:SS lower case and dd uppercase.
Final string and format was var date = moment("Wed Dec 24 00:00:00 -0800 2014","ddd MMM DD hh:mm:ss ZZ YYYY").format('DD');.
I ran this in a js fiddle and it provided me with 24, I think the issue is you didn't capitalize your .format('dd')
var date = moment("Wed Dec 24 00:00:00 -0800 2014","ddd MMM DD HH:MM:SS ZZ YYYY").format('DD');
alert(date);
Hi I have an AngularJS filter, which should return a date in a specifik format. However when I do the following:
var input = '2015-08-11T13:00:00';
var format = 'yyyy MMM dd - hh:mm:ss';
return $filter('date')(new Date(input), format);
it return date like this 2015 Aug 04 - 01:00:00 rather than this 2015 Aug 04 - 13:00:00 why is this, and is there a solution which would allow for any format to work?
https://docs.angularjs.org/api/ng/filter/date is your friend.
In particular:
'HH': Hour in day, padded (00-23)
'H': Hour in day (0-23)
'hh': Hour in AM/PM, padded (01-12)
'h': Hour in AM/PM, (1-12)
Take a look at this:
https://docs.angularjs.org/api/ng/filter/date
In order for you this to work in military time, change hh to HH.
Like you can see on this page, you need to use HH (capital letters) to show 24-hour hours.
I am convert the string to datetime using Date.parse and new Date(). For example if the string is "2/5/2012" then
var date=new Date(Date.parse("2/5/2012")); // Sun Feb 5 00:00:00 PST 2012
var date=new Date(Date.parse("2012/15/3")); //Sun Mar 3 00:00:00 PST 2013
Here my doubt is the string that i am giving takes the below format
"2/5/2012" - "mm/dd/yyyy"
"2012/15/3" - "yyyy/mm/dd"
I Want to know what are the format that i can use to define the date in string.
can i define string like this "3/2012/4"
Thanks in advance