or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

comparison-validation.mdcreation-parsing.mddisplay-formatting.mdduration.mdgetters-setters.mdindex.mdlocale.mdmanipulation.mdutilities.md

getters-setters.mddocs/

0

# Getters and Setters

1

2

Dual-purpose methods for reading and writing specific date/time components with support for all time units. These methods can both retrieve current values and set new values depending on whether arguments are provided.

3

4

## Capabilities

5

6

### Year Operations

7

8

Get or set the year component of the moment.

9

10

```javascript { .api }

11

/**

12

* Get the year (4 digits)

13

* @returns Year as number (e.g., 2023)

14

*/

15

year(): number;

16

17

/**

18

* Set the year

19

* @param y - Year to set (4 digit or 2 digit)

20

* @returns This moment instance (for chaining)

21

*/

22

year(y: number): Moment;

23

```

24

25

**Usage Examples:**

26

27

```javascript

28

import moment from "moment";

29

30

const date = moment("2023-12-25T15:30:00");

31

32

// Get year

33

console.log(date.year()); // 2023

34

35

// Set year

36

date.year(2024);

37

console.log(date.format()); // "2024-12-25T15:30:00"

38

39

// Chain with other operations

40

const newYear = moment().year(2025).month(0).date(1); // January 1, 2025

41

```

42

43

### Quarter Operations

44

45

Get or set the quarter of the year (1-4).

46

47

```javascript { .api }

48

/**

49

* Get the quarter (1-4)

50

* @returns Quarter as number

51

*/

52

quarter(): number;

53

54

/**

55

* Set the quarter

56

* @param q - Quarter to set (1-4)

57

* @returns This moment instance (for chaining)

58

*/

59

quarter(q: number): Moment;

60

```

61

62

**Usage Examples:**

63

64

```javascript

65

import moment from "moment";

66

67

const date = moment("2023-12-25T15:30:00");

68

69

// Get quarter

70

console.log(date.quarter()); // 4 (December is Q4)

71

72

// Set quarter (adjusts month to first month of quarter)

73

date.quarter(2);

74

console.log(date.format()); // "2023-04-25T15:30:00" (Q2 starts with April)

75

76

// Quarter boundaries

77

console.log(moment("2023-01-15").quarter()); // 1

78

console.log(moment("2023-04-15").quarter()); // 2

79

console.log(moment("2023-07-15").quarter()); // 3

80

console.log(moment("2023-10-15").quarter()); // 4

81

```

82

83

### Month Operations

84

85

Get or set the month component (0-11 for numbers, or month names for strings).

86

87

```javascript { .api }

88

/**

89

* Get the month (0-11, where 0 = January)

90

* @returns Month as number

91

*/

92

month(): number;

93

94

/**

95

* Set the month

96

* @param M - Month to set (0-11 as number, or month name as string)

97

* @returns This moment instance (for chaining)

98

*/

99

month(M: number|string): Moment;

100

```

101

102

**Usage Examples:**

103

104

```javascript

105

import moment from "moment";

106

107

const date = moment("2023-12-25T15:30:00");

108

109

// Get month

110

console.log(date.month()); // 11 (December is 11, zero-indexed)

111

112

// Set month by number (0-indexed)

113

date.month(0); // January

114

console.log(date.format()); // "2023-01-25T15:30:00"

115

116

// Set month by name

117

date.month("March");

118

console.log(date.format()); // "2023-03-25T15:30:00"

119

120

// Short month names also work

121

date.month("Dec");

122

console.log(date.format()); // "2023-12-25T15:30:00"

123

124

// Handle month overflow (adjusts to last day of month if needed)

125

const jan31 = moment("2023-01-31");

126

jan31.month(1); // February doesn't have 31 days

127

console.log(jan31.format()); // "2023-02-28T00:00:00" (adjusted to last day)

128

```

129

130

### Date Operations

131

132

Get or set the day of the month (1-31).

133

134

```javascript { .api }

135

/**

136

* Get the day of the month (1-31)

137

* @returns Day as number

138

*/

139

date(): number;

140

141

/**

142

* Set the day of the month

143

* @param d - Day to set (1-31)

144

* @returns This moment instance (for chaining)

145

*/

146

date(d: number): Moment;

147

```

148

149

**Usage Examples:**

150

151

```javascript

152

import moment from "moment";

153

154

const date = moment("2023-12-25T15:30:00");

155

156

// Get date

157

console.log(date.date()); // 25

158

159

// Set date

160

date.date(15);

161

console.log(date.format()); // "2023-12-15T15:30:00"

162

163

// Handle overflow (wraps to next month)

164

date.date(35);

165

console.log(date.format()); // "2024-01-04T15:30:00" (35 - 31 = 4 days into January)

166

167

// Set to last day of month

168

const lastDay = moment("2023-02-01").date(28);

169

console.log(lastDay.format()); // "2023-02-28T00:00:00"

170

```

171

172

### Day of Week Operations

173

174

Get or set the day of the week (0-6 where Sunday = 0, or day names).

175

176

```javascript { .api }

177

/**

178

* Get the day of the week (0-6, where 0 = Sunday)

179

* @returns Day of week as number

180

*/

181

day(): number;

182

183

/**

184

* Set the day of the week

185

* @param d - Day to set (0-6 as number, or day name as string)

186

* @returns This moment instance (for chaining)

187

*/

188

day(d: number|string): Moment;

189

190

/**

191

* Get locale-aware weekday (0-6, where first day depends on locale)

192

* @returns Weekday as number

193

*/

194

weekday(): number;

195

196

/**

197

* Set locale-aware weekday

198

* @param d - Weekday to set (0-6)

199

* @returns This moment instance (for chaining)

200

*/

201

weekday(d: number): Moment;

202

203

/**

204

* Get ISO weekday (1-7, where 1 = Monday)

205

* @returns ISO weekday as number

206

*/

207

isoWeekday(): number;

208

209

/**

210

* Set ISO weekday

211

* @param d - ISO weekday to set (1-7 as number, or day name as string)

212

* @returns This moment instance (for chaining)

213

*/

214

isoWeekday(d: number|string): Moment;

215

```

216

217

**Usage Examples:**

218

219

```javascript

220

import moment from "moment";

221

222

const date = moment("2023-12-25T15:30:00"); // Monday

223

224

// Get day of week

225

console.log(date.day()); // 1 (Monday, Sunday = 0)

226

console.log(date.weekday()); // Locale-dependent

227

console.log(date.isoWeekday()); // 1 (Monday, ISO standard)

228

229

// Set by number

230

date.day(5); // Friday

231

console.log(date.format()); // "2023-12-29T15:30:00"

232

233

// Set by name

234

date.day("Wednesday");

235

console.log(date.format()); // "2023-12-27T15:30:00"

236

237

// ISO weekday (Monday = 1)

238

date.isoWeekday(7); // Sunday

239

console.log(date.format()); // "2023-12-31T15:30:00"

240

241

// Weekday names work with isoWeekday too

242

date.isoWeekday("Monday");

243

console.log(date.format()); // "2023-12-25T15:30:00"

244

```

245

246

### Hour Operations

247

248

Get or set the hour component (0-23).

249

250

```javascript { .api }

251

/**

252

* Get the hour (0-23)

253

* @returns Hour as number

254

*/

255

hour(): number;

256

257

/**

258

* Set the hour

259

* @param h - Hour to set (0-23)

260

* @returns This moment instance (for chaining)

261

*/

262

hour(h: number): Moment;

263

264

// Alias methods

265

hours(): number;

266

hours(h: number): Moment;

267

```

268

269

**Usage Examples:**

270

271

```javascript

272

import moment from "moment";

273

274

const date = moment("2023-12-25T15:30:00");

275

276

// Get hour

277

console.log(date.hour()); // 15

278

console.log(date.hours()); // 15 (alias)

279

280

// Set hour

281

date.hour(9);

282

console.log(date.format()); // "2023-12-25T09:30:00"

283

284

// 24-hour format

285

date.hour(23);

286

console.log(date.format()); // "2023-12-25T23:30:00"

287

288

// Handle overflow (wraps to next day)

289

date.hour(25);

290

console.log(date.format()); // "2023-12-26T01:30:00"

291

```

292

293

### Minute Operations

294

295

Get or set the minute component (0-59).

296

297

```javascript { .api }

298

/**

299

* Get the minute (0-59)

300

* @returns Minute as number

301

*/

302

minute(): number;

303

304

/**

305

* Set the minute

306

* @param m - Minute to set (0-59)

307

* @returns This moment instance (for chaining)

308

*/

309

minute(m: number): Moment;

310

311

// Alias methods

312

minutes(): number;

313

minutes(m: number): Moment;

314

```

315

316

**Usage Examples:**

317

318

```javascript

319

import moment from "moment";

320

321

const date = moment("2023-12-25T15:30:00");

322

323

// Get minute

324

console.log(date.minute()); // 30

325

console.log(date.minutes()); // 30 (alias)

326

327

// Set minute

328

date.minute(45);

329

console.log(date.format()); // "2023-12-25T15:45:00"

330

331

// Handle overflow (wraps to next hour)

332

date.minute(75);

333

console.log(date.format()); // "2023-12-25T16:15:00"

334

```

335

336

### Second Operations

337

338

Get or set the second component (0-59).

339

340

```javascript { .api }

341

/**

342

* Get the second (0-59)

343

* @returns Second as number

344

*/

345

second(): number;

346

347

/**

348

* Set the second

349

* @param s - Second to set (0-59)

350

* @returns This moment instance (for chaining)

351

*/

352

second(s: number): Moment;

353

354

// Alias methods

355

seconds(): number;

356

seconds(s: number): Moment;

357

```

358

359

**Usage Examples:**

360

361

```javascript

362

import moment from "moment";

363

364

const date = moment("2023-12-25T15:30:45");

365

366

// Get second

367

console.log(date.second()); // 45

368

console.log(date.seconds()); // 45 (alias)

369

370

// Set second

371

date.second(30);

372

console.log(date.format()); // "2023-12-25T15:30:30"

373

374

// Handle overflow (wraps to next minute)

375

date.second(75);

376

console.log(date.format()); // "2023-12-25T15:31:15"

377

```

378

379

### Millisecond Operations

380

381

Get or set the millisecond component (0-999).

382

383

```javascript { .api }

384

/**

385

* Get the millisecond (0-999)

386

* @returns Millisecond as number

387

*/

388

millisecond(): number;

389

390

/**

391

* Set the millisecond

392

* @param ms - Millisecond to set (0-999)

393

* @returns This moment instance (for chaining)

394

*/

395

millisecond(ms: number): Moment;

396

397

// Alias methods

398

milliseconds(): number;

399

milliseconds(ms: number): Moment;

400

```

401

402

**Usage Examples:**

403

404

```javascript

405

import moment from "moment";

406

407

const date = moment("2023-12-25T15:30:45.123");

408

409

// Get millisecond

410

console.log(date.millisecond()); // 123

411

console.log(date.milliseconds()); // 123 (alias)

412

413

// Set millisecond

414

date.millisecond(500);

415

console.log(date.format("YYYY-MM-DD HH:mm:ss.SSS")); // "2023-12-25 15:30:45.500"

416

417

// Handle overflow (wraps to next second)

418

date.millisecond(1500);

419

console.log(date.format("YYYY-MM-DD HH:mm:ss.SSS")); // "2023-12-25 15:30:46.500"

420

```

421

422

### Week Operations

423

424

Get or set week-related values.

425

426

```javascript { .api }

427

/**

428

* Get the week of the year (1-53, locale-aware)

429

* @returns Week number

430

*/

431

week(): number;

432

433

/**

434

* Set the week of the year

435

* @param d - Week number to set (1-53)

436

* @returns This moment instance (for chaining)

437

*/

438

week(d: number): Moment;

439

440

/**

441

* Get the ISO week of the year (1-53)

442

* @returns ISO week number

443

*/

444

isoWeek(): number;

445

446

/**

447

* Set the ISO week of the year

448

* @param d - ISO week number to set (1-53)

449

* @returns This moment instance (for chaining)

450

*/

451

isoWeek(d: number): Moment;

452

453

/**

454

* Get the week year (locale-aware)

455

* @returns Week year as number

456

*/

457

weekYear(): number;

458

459

/**

460

* Set the week year

461

* @param d - Week year to set

462

* @returns This moment instance (for chaining)

463

*/

464

weekYear(d: number): Moment;

465

466

/**

467

* Get the ISO week year

468

* @returns ISO week year as number

469

*/

470

isoWeekYear(): number;

471

472

/**

473

* Set the ISO week year

474

* @param d - ISO week year to set

475

* @returns This moment instance (for chaining)

476

*/

477

isoWeekYear(d: number): Moment;

478

479

/**

480

* Get the day of the year (1-366)

481

* @returns Day of year as number

482

*/

483

dayOfYear(): number;

484

485

/**

486

* Set the day of the year

487

* @param d - Day of year to set (1-366)

488

* @returns This moment instance (for chaining)

489

*/

490

dayOfYear(d: number): Moment;

491

492

// Alias methods

493

weeks(): number;

494

weeks(d: number): Moment;

495

isoWeeks(): number;

496

isoWeeks(d: number): Moment;

497

```

498

499

**Usage Examples:**

500

501

```javascript

502

import moment from "moment";

503

504

const date = moment("2023-12-25T15:30:00");

505

506

// Week operations

507

console.log(date.week()); // Week number (locale-dependent)

508

console.log(date.isoWeek()); // 52 (ISO week)

509

console.log(date.weekYear()); // 2023

510

console.log(date.isoWeekYear()); // 2023

511

512

// Day of year

513

console.log(date.dayOfYear()); // 359 (December 25 is the 359th day)

514

515

// Set week

516

date.week(1); // First week of year

517

console.log(date.format()); // Date adjusted to first week

518

519

// Set day of year

520

date.dayOfYear(100); // 100th day of year

521

console.log(date.format()); // "2023-04-10T15:30:00" (April 10)

522

523

// ISO week operations

524

const isoDate = moment("2023-01-01");

525

console.log(isoDate.isoWeek()); // ISO week number

526

isoDate.isoWeek(26); // Set to week 26

527

console.log(isoDate.format()); // Date adjusted to week 26

528

```

529

530

### Generic Get/Set Methods

531

532

Generic methods for getting and setting any time unit.

533

534

```javascript { .api }

535

/**

536

* Get value for any time unit

537

* @param unit - Time unit to get

538

* @returns Value for the specified unit

539

*/

540

get(unit: unitOfTime.All): number;

541

542

/**

543

* Set value for any time unit

544

* @param unit - Time unit to set

545

* @param value - Value to set

546

* @returns This moment instance (for chaining)

547

*/

548

set(unit: unitOfTime.All, value: number): Moment;

549

550

/**

551

* Set multiple time units at once

552

* @param objectLiteral - Object containing unit-value pairs

553

* @returns This moment instance (for chaining)

554

*/

555

set(objectLiteral: MomentSetObject): Moment;

556

557

interface MomentSetObject extends MomentInputObject {

558

weekYears?: numberlike;

559

weekYear?: numberlike;

560

gg?: numberlike;

561

isoWeekYears?: numberlike;

562

isoWeekYear?: numberlike;

563

GG?: numberlike;

564

quarters?: numberlike;

565

quarter?: numberlike;

566

Q?: numberlike;

567

weeks?: numberlike;

568

week?: numberlike;

569

w?: numberlike;

570

isoWeeks?: numberlike;

571

isoWeek?: numberlike;

572

W?: numberlike;

573

dayOfYears?: numberlike;

574

dayOfYear?: numberlike;

575

DDD?: numberlike;

576

weekdays?: numberlike;

577

weekday?: numberlike;

578

e?: numberlike;

579

isoWeekdays?: numberlike;

580

isoWeekday?: numberlike;

581

E?: numberlike;

582

}

583

```

584

585

**Usage Examples:**

586

587

```javascript

588

import moment from "moment";

589

590

const date = moment("2023-12-25T15:30:45.123");

591

592

// Generic get operations

593

console.log(date.get("year")); // 2023

594

console.log(date.get("month")); // 11 (December)

595

console.log(date.get("date")); // 25

596

console.log(date.get("hour")); // 15

597

console.log(date.get("minute")); // 30

598

console.log(date.get("second")); // 45

599

console.log(date.get("millisecond")); // 123

600

601

// Alternative unit names

602

console.log(date.get("y")); // 2023 (year)

603

console.log(date.get("M")); // 11 (month)

604

console.log(date.get("d")); // 25 (date)

605

console.log(date.get("h")); // 15 (hour)

606

console.log(date.get("m")); // 30 (minute)

607

console.log(date.get("s")); // 45 (second)

608

console.log(date.get("ms")); // 123 (millisecond)

609

610

// Generic set operations

611

date.set("year", 2024);

612

date.set("month", 5); // June (0-indexed)

613

date.set("date", 15);

614

console.log(date.format()); // "2024-06-15T15:30:45"

615

616

// Set multiple values at once

617

date.set({

618

year: 2025,

619

month: 11, // December

620

date: 31,

621

hour: 23,

622

minute: 59,

623

second: 59

624

});

625

console.log(date.format()); // "2025-12-31T23:59:59"

626

627

// Use shorter unit names

628

date.set({

629

y: 2023,

630

M: 0, // January

631

d: 1,

632

h: 0,

633

m: 0,

634

s: 0,

635

ms: 0

636

});

637

console.log(date.format()); // "2023-01-01T00:00:00"

638

639

// Week-based setting

640

date.set("week", 26);

641

date.set("weekday", 1); // Monday

642

console.log(date.format()); // Date adjusted to Monday of week 26

643

644

// Day of year setting

645

date.set("dayOfYear", 182); // Middle of year

646

console.log(date.format()); // Date adjusted to day 182

647

```

648

649

## Chaining Operations

650

651

All setter methods return the Moment instance, allowing for method chaining.

652

653

```javascript

654

import moment from "moment";

655

656

// Chain multiple setters

657

const customDate = moment()

658

.year(2024)

659

.month(5) // June

660

.date(15)

661

.hour(14)

662

.minute(30)

663

.second(0)

664

.millisecond(0);

665

666

console.log(customDate.format()); // "2024-06-15T14:30:00"

667

668

// Mix with other operations

669

const result = moment("2023-01-01")

670

.month(11) // December

671

.date(25) // Christmas

672

.hour(18) // 6 PM

673

.add(1, "year") // 2024

674

.startOf("day") // Beginning of day

675

.add(12, "hours"); // Noon

676

677

console.log(result.format()); // "2024-12-25T12:00:00"

678

679

// Complex chaining example

680

const complexDate = moment()

681

.set({

682

year: 2023,

683

month: 0, // January

684

date: 1

685

})

686

.dayOfYear(100) // 100th day

687

.hour(15)

688

.minute(30)

689

.startOf("minute"); // Reset seconds/milliseconds

690

691

console.log(complexDate.format()); // Date representing 100th day at 15:30:00

692

```