How can I format a date with momentjs without leading zeros? E.g. 2018-6-29 instead of 2018-06-29.
If the number is smaller than 10 it should not add 0 before it.
According to the docs (https://momentjs.com/docs/#/parsing/string-format/) if you use M or D instead of MM or DD in the format() function you will get the date without the 0.
moment().format("YYYY-M-DD") is what you are after.
if you also want to exclude the 0 from single digit days you can use:
moment().format("YYYY-M-D")
(fiddle here: http://jsfiddle.net/rLjQx/69671/)
You should use the format string YYYY-M-D.
Consult the documentation of how to format moments. https://momentjs.com/docs/#/displaying/format/
Related
I tried to convert a time-stamp ("1985-02-07T00:00:00.000Z") to a date and I failed to succeed in my several different attempts.
Below is the query I have tried:
select to_date('1985-02-07T00:00:00.000Z', 'YYYY-MM-DDTHH24:MI:SS.fffZ')
from dual;
Your suggestions are greatly appreciated.
to_date converts the input to a DATE type which does not support fractional seconds. To use fractional seconds you need to use a TIMESTAMP type which is created when using to_timestamp
pst's comment about the ff3 modifier is also correct.
"Constant" values in the format mask need to be enclosed in double quote
So the final statement is:
select to_timestamp('1985-02-07T00:00:00.000Z', 'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"')
from dual;
SQL> select cast(to_timestamp('1985-02-07T00:00:00.000Z', 'yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') as date)
2 from dual
3 /
CAST(TO_TIMESTAMP('
-------------------
07-02-1985 00:00:00
1 row selected.
Some rules to follow:
Literals must be double-quoted: MM expects a month number, "MM" expects a double-M.
The format for fractional seconds is FF, not F or FFF. You specify the number of digits with a trailing integer, e.g. FF3.
But dates cannot hold fractional seconds anyway so you cannot use FF3 in this context.
This works:
SELECT TO_DATE('1985-02-07T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS')
FROM dual;
I don't know if there's a way to ignore fractional seconds in TO_DATE() so I've used string manipulation functions to strip them out:
SELECT TO_DATE(SUBSTR('1985-02-07T00:00:00.000Z', 1, 19), 'YYYY-MM-DD"T"HH24:MI:SS')
FROM dual;
SELECT to_timestamp_tz('2012-08-08T09:06:14.000-07:00','YYYY-MM-DD"T"HH24:MI:SS.FF3TZR')
FROM dual;
External table DDL,
extract_date char(29) DATE_FORMAT timestamp WITH TIMEZONE mask 'YYYY-MM-DD"T"HH24:MI:SS.FF3TZR'
if u want to get the string in datetime format then try this....
select to_char(TO_DATE('2012-06-26T00:00:00.809Z', 'YYYY-MM-DD"T"HH24:MI:SS".""ZZZZ"'),'yyyy-MM-dd hh:mm:ss PM') as EVENT_DATE from dual
EVENT_DATE
-----------------------
2012-06-26 12:06:00 AM
only for date simply use...
select TO_DATE('2012-01-06T00:00:00.809Z', 'YYYY-MM-DD"T"HH24:MI:SS".""ZZZZ"') from dual
I am trying to compare two dates but always it gives me opposite result.
I am trying to compare below dates
var tocompare=09/22/2017 and var insurenceexpiry=04/02/2018
I tried to compare as below.
console.log(insurenceexpiry > tocompare);
console.log(insurenceexpiry < tocompare);
which gives me false and true. As per my knowledge insurenceexpiry is greater and when i compare as insurenceexpiry > tocompare it should give me true but I am getting false. What I am doing wrong?
You have to convert it into dates using new Date(datestring).
Otherwise 9/22/2017 without quotes will do math operations
var tocompare=new Date("09/22/2017");
var insurenceexpiry=new Date("04/02/2018");
console.log(insurenceexpiry > tocompare);
console.log(insurenceexpiry < tocompare);
If you want to compare 2 dates as string directly, then use YYYY/MM/DD format, (you can use any separator apart from /, the main thing is YYYYMMDD)
Otherwise parse them as date (as per i--'s answer) and then compare, as a month or day can be bigger than another date, but that doesn't meant that it's actually a bigger date, so you cannot use mmddyyyy format for a simple string comparison to get which date is bigger.
I am trying to fetch the video data from YouTube API.
This is my request:
https://www.googleapis.com/youtube/v3/search?key={{YOUKEY}}&channelId={{CHANNELID}}&part=snippet,id&order=date&maxResults=50&publishedAfter=2014-09-21T00:00:00Z&publishedBefore=2014-09-22T02:00:00Z
As you can see in this, the publishedAfter is in this way
2014-09-21T00:00:00Z with time set to zero. I tried using moment, but couldn't get the exact format.
the default moment.format() Date is displayed like you ask, however you could force it by using a format string like
moment(string).format('YYYY-MM-DDThh:mm:ss[Z]');
explanation
YYYY year in digits
MM month in digits (base 1)
DD day of year in digits
T a random letter
hh:mm:ss hour:minutes:seconds
[Z] escape sequence for printing the letter Z
the letter Z in the format string in fact prints the time zone, as seen in the docs.
the exact solution is
var formatString = 'YYYY-MM-DDT[00:00:00Z]';
moment(string).format(formatString);
fiddle
I have problem with String > Format of timezone.
I have string: " 2015-02-10 00:00:00,3,UTC "
And try to format it in moment:
moment('2015-02-10 00:00:00,3,UTC', 'YYYY-MM-DD HH:mm:ss, ?, ?')
What should I insert instead of "?"
It's not possible to format that kind of string into moment because a lone 3 does not designate the timezone offset in any standard format.
You need to change the 3 into +0030.
This should work:
var date = '2015-02-10 00:00:00,3,UTC'
.replace(/,(\d\d),/,',+$100,') // for double digit cases (11 turns to +1100)
.replace(/,(\d),/,',+0$100,'); // single digit cases (3 to +0300)
And then
moment(date, 'YYYY-MM-DD HH:mm:ss,ZZ')
I'm not sure what the UTC part is about since +0300 is clearly not UTC. I guess it's just saying that the the 3 hour offset is relative to UTC?
I have a date formatted in Moment.JS but when I call
currentDay.month()
//Or
currentDay.date()
I get a 1 digit number (for example Jan 1st would be 0 for month and 1 for day). I need to get this with a leading 0 for date and month if less than 10. I can do this with something like...
currentDay.date() >= 10 ? currentDay.date() : '0'+currentDay.date()
But I was looking for something a bit nicer. Can I do this easily with MomentJS? I don't see anything in docs.
You can do this, eg for the month, with
moment().format('MM')