首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用dayjs获取beforeDate

使用dayjs获取beforeDate
EN

Stack Overflow用户
提问于 2021-10-26 10:47:31
回答 3查看 144关注 0票数 1

如何使用DayJs获取当前日期之前的日期。

我知道如何获取当前日期,但我们可以从当前日期中删除15天吗?

代码语言:javascript
复制
cy.get('input[name="day"]').should('have.value', (Cypress.dayjs().format('DD')))
cy.get('input[name="month"]').should('have.value', (Cypress.dayjs().format('MM')))
 cy.get('input[name="year"]').should('have.value', (Cypress.dayjs().format('YY'))) ```


This is my code for getting the currentDate. I would like to get 15 days before my current Date. I would have used substract if the date was one input field but here we have different placeholders for the inputs.
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-10-27 10:17:25

对于可读性测试,只需计算一次目标。

代码语言:javascript
复制
const target = dayjs().subtract(15, 'day')

cy.get('input[name="day"]').should('have.value', target.format('DD'))
cy.get('input[name="month"]').should('have.value', target.format('MM'))
cy.get('input[name="year"]').should('have.value', target .format('YY'))
票数 0
EN

Stack Overflow用户

发布于 2021-10-26 10:56:13

documentation

代码语言:javascript
复制
dayjs().subtract(15, 'day');

可用的单元都提到了hereday就是其中之一。

票数 2
EN

Stack Overflow用户

发布于 2021-10-26 11:06:38

正如@pavelsaman提到的那样,.subtract将会工作。您只需将其格式化为提取日、月、年。

代码语言:javascript
复制
const dayjs = require('dayjs')

cy.get('input[name="day"]').should(
  'have.value',
  dayjs().subtract(15, 'day').format('DD')
) //11
cy.get('input[name="month"]').should(
  'have.value',
  dayjs().subtract(15, 'day').format('MM')
) //10
cy.get('input[name="year"]').should(
  'have.value',
  dayjs().subtract(15, 'day').format('YY')
) //21
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69721679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档